我如何获得 azure 函数查询的请求费用?

how do I get the request charge for azure function query?

我想知道是否可以通过以下代码使用 cosmosdb 输入绑定在 azure 函数中获取请求费用:

        [FunctionName("RecipientRead")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "recipient/{partitionKey}/{id}")] HttpRequest req,
            [CosmosDB(
                databaseName: "RecipientDatabase",
                collectionName: "RecipientCollection",
                ConnectionStringSetting = "CosmosDbConnectionString",
                Id = "{id}",
                PartitionKey = "{partitionKey}")] Recipient recipient,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            return new OkObjectResult(recipient);
        }

我可以这样获取:

[FunctionName("RecipientRead")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "recipient/{partitionKey}/{id}")] HttpRequest req,
             [CosmosDB(
                databaseName: "RecipientDatabase",
                collectionName: "RecipientCollection",
                ConnectionStringSetting = "CosmosDbConnectionString",
                Id = "{id}",
                PartitionKey = "{partitionKey}")] Recipient rt,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");
            
            CosmosClient client = new CosmosClient("AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==", new CosmosClientOptions() { ApplicationName = "CosmosDBDotnetQuickstart" });
            Database db = client.GetDatabase("RecipientDatabase");
            Container c = db.GetContainer("RecipientCollection");

            ItemResponse<Recipient> r = await c.ReadItemAsync<Recipient>("1", new PartitionKey("1"));

            double rc = r.RequestCharge;

            return new OkObjectResult("ok");
        }

目前无法根据函数API合约获取。

请不要在函数执行中创建自己的 CosmosClient 实例(参见 https://docs.microsoft.com/en-us/azure/azure-functions/manage-connections?tabs=csharp#azure-cosmos-db-clients),而是利用绑定:

[CosmosDB(
                databaseName: "RecipientDatabase",
                collectionName: "RecipientCollection",
                ConnectionStringSetting = "CosmosDbConnectionString")] DocumentClient client,

或者,如果您使用 4.X 扩展程序:

[CosmosDB(
                databaseName: "RecipientDatabase",
                containerName: "RecipientCollection",
                Connection= "CosmosDbConnectionString")] CosmosClient client,

或者您可以决定利用 Azure Functions DI https://github.com/Azure/azure-cosmos-dotnet-v3/tree/master/Microsoft.Azure.Cosmos.Samples/Usage/AzureFunctions