mcwrath
mcwrath

Reputation: 45

Azure documentDB - id conflicts not throwing exception

I am writing a simple API that posts a json document to a single collection in DocumentDB. What is strange, is that I get no exception or indication of error when I try to add a document with the same id more than once.

    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...
        }
    }

The behavior I see is that the first document saves. I can see it in the document explorer. Then if I change data and keep the same id, the code appears to work, but the updated document does not actually get saved, however I dont get an exception as expected. Am I wrong to think there should be an exception here?

Upvotes: 1

Views: 1629

Answers (1)

Andrew Liu
Andrew Liu

Reputation: 8119

In the event of a conflict, DocumentDB throws a Microsoft.Azure.Documents.DocumentClientException with the message: {"Errors":["Resource with specified id or name already exists"]}.

The reason (most likely) you are not seeing the exception is that the code you have runs asynchronously. In other words, your code may be ending execution before the result of the create operation has returned. You can resolve the async method simply by calling .Result.

Upvotes: 1

Related Questions