我想知道是否有可能在使用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");
}
目前还没有办法基于Functions API契约获得它。
请不要在Function执行中创建自己的CosmosClient实例(参见https://learn.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,
或者你可以决定使用AzureFunctions DI https://github.com/Azure/azure-cosmos-dotnet-v3/tree/master/Microsoft.Azure.Cosmos.Samples/Usage/AzureFunctions