gpt4 book ai didi

c# - 使用 Microsoft.Azure.Cosmos 的 FeedResponse 时如何获取单个文档的 ETag

转载 作者:行者123 更新时间:2023-12-02 06:01:38 27 4
gpt4 key购买 nike

我正在查看来自 https://github.com/Azure/azure-cosmos-dotnet-v3/blob/dc3468bd5ce828e504ddef92ef792c35370de055/Microsoft.Azure.Cosmos.Samples/Usage/ItemManagement/Program.cs#L590 的样本

下面是使用container.ReadItemAsync时使用ETag的示例:

            ItemResponse<SalesOrder> itemResponse = await container.ReadItemAsync<SalesOrder>(
partitionKey: new PartitionKey("Account1"),
id: "SalesOrder1");

Console.WriteLine("ETag of read item - {0}", itemResponse.ETag);

SalesOrder item = itemResponse;
//Update the total due
itemResponse.Resource.TotalDue = 1000000;

//persist the change back to the server
ItemResponse<SalesOrder> updatedDoc = await container.ReplaceItemAsync<SalesOrder>(
partitionKey: new PartitionKey(item.AccountNumber),
id: item.Id,
item: item);

Console.WriteLine("ETag of item now that is has been updated - {0}", updatedDoc.ETag);

//now, using the originally retrieved item do another update
//but set the AccessCondition class with the ETag of the originally read item and also set the AccessConditionType
//this tells the service to only do this operation if ETag on the request matches the current ETag on the item
//in our case it won't, because we updated the item and therefore gave it a new ETag
try
{
itemResponse.Resource.TotalDue = 9999999;
updatedDoc = await container.ReplaceItemAsync<SalesOrder>(itemResponse, item.Id, new PartitionKey(item.AccountNumber), new ItemRequestOptions { IfMatchEtag = itemResponse.ETag });
}
catch (CosmosException cre)
{
// now notice the failure when attempting the update
// this is because the ETag on the server no longer matches the ETag of doc (b/c it was changed in step 2)
if (cre.StatusCode == HttpStatusCode.PreconditionFailed)
{
Console.WriteLine("As expected, we have a pre-condition failure exception\n");
}
}

在我的场景中,我有一个查询,我需要获取 ETag 以便稍后在更新中使用。在此示例中,我们得到 FeedResponse<SalesOrder>其中有 ETag,但这是与最后一笔交易相关的标签,而不是文档。所以我的问题是如何获取单个文档的 ETag?

        private static async Task QueryItems()
{
//******************************************************************************************************************
// 1.4 - Query for items by a property other than Id
//
// NOTE: Operations like AsEnumerable(), ToList(), ToArray() will make as many trips to the database
// as required to fetch the entire result-set. Even if you set MaxItemCount to a smaller number.
// MaxItemCount just controls how many results to fetch each trip.
//******************************************************************************************************************
Console.WriteLine("\n1.4 - Querying for a item using its AccountNumber property");

QueryDefinition query = new QueryDefinition(
"select * from sales s where s.AccountNumber = @AccountInput ")
.WithParameter("@AccountInput", "Account1");

FeedIterator<SalesOrder> resultSet = container.GetItemQueryIterator<SalesOrder>(
query,
requestOptions: new QueryRequestOptions()
{
PartitionKey = new PartitionKey("Account1"),
MaxItemCount = 1
});

List<SalesOrder> allSalesForAccount1 = new List<SalesOrder>();
while (resultSet.HasMoreResults)
{
FeedResponse<SalesOrder> response = await resultSet.ReadNextAsync();
SalesOrder sale = response.First();
Console.WriteLine($"\n1.4.1 Account Number: {sale.AccountNumber}; Id: {sale.Id};");
if(response.Diagnostics != null)
{
Console.WriteLine($" Diagnostics {response.Diagnostics.ToString()}");
}

allSalesForAccount1.Add(sale);
}

最佳答案

执行查询时,FeedResponse 包含 T 类型的对象列表。如果 T 包含与 _etag 系统属性匹配的属性,则可以获得 ETag。 Source Github issue comment .

例如,使用 NewtonSoft.JSON 的 JsonProperty 属性,我必须将以下属性添加到我的类型 T

[JsonProperty("_etag")]
public string ETag { get; set; }

关于c# - 使用 Microsoft.Azure.Cosmos 的 FeedResponse 时如何获取单个文档的 ETag,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62002583/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com