I'm wondering if its possible to get the request charge in an azure function using cosmosdb input binding, from the below code:
[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);
}
I am able to get it this way:
[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");
}
CodePudding user response:
Currently there is no way to obtain it based on Functions API contracts.
Please do not create your own CosmosClient instance inside the Function execution (see https://docs.microsoft.com/en-us/azure/azure-functions/manage-connections?tabs=csharp#azure-cosmos-db-clients), instead, leverage the binding:
[CosmosDB(
databaseName: "RecipientDatabase",
collectionName: "RecipientCollection",
ConnectionStringSetting = "CosmosDbConnectionString")] DocumentClient client,
Or if you use the 4.X Extension:
[CosmosDB(
databaseName: "RecipientDatabase",
containerName: "RecipientCollection",
Connection= "CosmosDbConnectionString")] CosmosClient client,
Or you can decide to leverage Azure Functions DI https://github.com/Azure/azure-cosmos-dotnet-v3/tree/master/Microsoft.Azure.Cosmos.Samples/Usage/AzureFunctions