gpt4 book ai didi

.net - Cosmos DB : Collection exists, 但抛出 410 GONE httpstatus 代码

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

我的一些代码在我的应用程序启动时检查集合是否存在。

但是,要检查的代码:

await client.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(DatabaseId, collectionId));

抛出 DocumentClientException 并带有 410 GONE http 状态代码。

但是,当我使用数据浏览器时,我发现具有我提供的 collectionId 的集合确实存在。此外,我在测试中尝试了以下操作:

try
{
await client.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(DatabaseId, collectionId));
}
catch (DocumentClientException e)
{

if (e.StatusCode == System.Net.HttpStatusCode.NotFound || e.StatusCode == System.Net.HttpStatusCode.Gone)
{
var collection = new DocumentCollection { Id = collectionId, ResourceId = collectionId };
await client.CreateDocumentCollectionAsync(
UriFactory.CreateDatabaseUri(DatabaseId),
collection,
new RequestOptions { OfferThroughput = 1000 });
}
else
{
throw;
}
}

此代码仍然抛出 DocumentClientException 和 410 状态代码。但是,当我尝试创建文档集合时,失败并显示 具有指定 id 或名称的资源已存在

我的搜索是否错误?我确实手动创建了集合,所以集合ID可能不同,但似乎很奇怪我无法在代码之外设置文档集合ID。

最佳答案

我建议使用DocumentCollectionQuery来检查它是否存在。 ReadDocumentCollectionAsync 并不完全相同。

您可以在 Cosmosnaut 的 CosmosCollectionCreator.cs 中查看它是如何完成的.

只需使用查询即可

var collection = _documentClient
.CreateDocumentCollectionQuery(database.SelfLink)
.ToArray()
.FirstOrDefault(c => c.Id == collectionName);

然后创建它

collection = await _documentClient.CreateDocumentCollectionAsync(database.SelfLink, collection, new RequestOptions
{
OfferThroughput = collectionThroughput
});

关于.net - Cosmos DB : Collection exists, 但抛出 410 GONE httpstatus 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50295597/

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