Azure documentDB - id 冲突不抛出异常
Azure documentDB - id conflicts not throwing exception
我正在编写一个简单的 API 将 json 文档发布到 DocumentDB 中的单个集合。奇怪的是,当我尝试多次添加具有相同 ID 的文档时,我没有收到任何异常或错误指示。
public static async Task<ResourceResponse<Document>> CreateDocument(Database database, DocumentCollection collection, object obj)
{
try
{
Uri collUri = UriFactory.CreateDocumentCollectionUri(database.Id, collection.Id);
return await client.CreateDocumentAsync(collUri, obj, null, true);
}
catch (DocumentClientException e)
{
// nothing is ever caught...
}
}
我看到的行为是保存第一个文档。我可以在文档资源管理器中看到它。然后,如果我更改数据并保持相同的 ID,代码似乎可以工作,但更新后的文档实际上并没有得到保存,但是我没有像预期的那样得到异常。我认为这里应该有例外是错误的吗?
如果发生冲突,DocumentDB 将抛出 Microsoft.Azure.Documents.DocumentClientException
消息:{"Errors":["Resource with specified id or name already exists"]}
.
您没有看到异常的原因(很可能)是您的代码异步运行。换句话说,您的代码可能会在创建操作的结果返回之前结束执行。您只需调用 .Result
.
即可解析异步方法
我正在编写一个简单的 API 将 json 文档发布到 DocumentDB 中的单个集合。奇怪的是,当我尝试多次添加具有相同 ID 的文档时,我没有收到任何异常或错误指示。
public static async Task<ResourceResponse<Document>> CreateDocument(Database database, DocumentCollection collection, object obj)
{
try
{
Uri collUri = UriFactory.CreateDocumentCollectionUri(database.Id, collection.Id);
return await client.CreateDocumentAsync(collUri, obj, null, true);
}
catch (DocumentClientException e)
{
// nothing is ever caught...
}
}
我看到的行为是保存第一个文档。我可以在文档资源管理器中看到它。然后,如果我更改数据并保持相同的 ID,代码似乎可以工作,但更新后的文档实际上并没有得到保存,但是我没有像预期的那样得到异常。我认为这里应该有例外是错误的吗?
如果发生冲突,DocumentDB 将抛出 Microsoft.Azure.Documents.DocumentClientException
消息:{"Errors":["Resource with specified id or name already exists"]}
.
您没有看到异常的原因(很可能)是您的代码异步运行。换句话说,您的代码可能会在创建操作的结果返回之前结束执行。您只需调用 .Result
.