当我使用CreateItemAsync时,我得到了一个ItemResponse,它允许我访问RU中的RequestCharge,并可能记录它,这样我就可以准确地了解我对RU的使用。
但是,当使用LINQ使用CosmosClient查询时,我看不到任何获得RequestCharge的方法。查看RequestCharge是非常有用的,但它似乎是错误的,只有当我以某种方式查询时它才可用,所以我想我肯定遗漏了什么。
以下是我的代码示例。
var tenantContainer = cosmos.GetContainer("myapp", "tenant");
var query = tenantContainer.GetItemLinqQueryable<Tenant>(true, null,
new QueryRequestOptions { PartitionKey = new PartitionKey("all") })
.Where(r => r.AccountId = "1234");
var tenants = query.ToList();
//track.Metric("GetTenants", cosmosResponse.RequestCharge);注意,我使用的是“新的”CosmosClient,而不是旧的DocumentClient。
发布于 2020-06-29 03:19:19
请确保包含此using语句。
using Microsoft.Azure.Cosmos.Linq;然后,您可以使用包含带有RequestCharge的属性的.ToFeedIterator()。
下面是完整的代码示例:
var container = _cosmos.GetContainer("mydb", "user");
// Normal linq query
var query = container.GetItemLinqQueryable<Shared.Models.User>(true, null,
new QueryRequestOptions { PartitionKey = new PartitionKey(tenantName) })
.Where(r => r.Email == loginRequest.Email);
// Instead of getting the result, first convert to feed iterator
var iterator = query.ToFeedIterator();
// And finally execute with this command that also supports paging
var cosmosResponse = await iterator.ReadNextAsync();
// And then the RequestCharge is readily available
_track.Metric("GetUserForAuthentication", cosmosResponse.RequestCharge);
// And whatever linq execution you wanted to do, you can do on the response
var user = cosmosResponse.FirstOrDefault();https://stackoverflow.com/questions/62622107
复制相似问题