0
votes

Not sure what I'm doing wrong here, but I'm trying to insert a document into my-documents in the storage database my-db but this isn't happening.

I create a document client like this-

  _endpointUri = new Uri(Properties.Settings.Default.DocumentDBEndpoint);
  _privateKey = Properties.Settings.Default.DocumentDBKey;
  _databaseName = Properties.Settings.Default.DocumentDBDatabase;
  _collectionName = collectionName;
  _collectionUri = UriFactory.CreateDocumentCollectionUri(_databaseName, _collectionName);

  _documentClient = new DocumentClient(_endpointUri, _privateKey);

I then try to insert a document into the collection.

public async Task Set(T document)
{
  await _documentClient.CreateDocumentAsync(_collectionUri.ToString(), document);
}

I tried passing the _collectionUri as well as the toString and they both do the same thing. If I step through the debugger the toString of that collection uri looks like-

dbs/my-db/colls/my-documents

I've tried set .ConfigureAwait(false) on the call, as well as not awaiting it at all. Of course when it's not awaited it steps over the line fine, but no document ever gets added to the my-documents collection.

I'm not sure if i'm using CreateDocumentAsync (also tried UpsertDocumentAsync) because not errors are thrown.

Can anyone see from the information given what mistake I'm making?

Edit In my testing with Azure I did opt to use DocumentDB with MongoDB Api. That may be related. Since I can easily blow it all away I will recreate it using the DocumentDB Api and see if that fixes the problem.

1
Creating a database with MongoDB compatibility shouldn't affect your ability to make native calls via DocumentDB protocol + related SDK's. - David Makogon

1 Answers

0
votes

This turned out to be two different problems I assumed were the same.

Document Not Creating-

So I set this up originally to use the MongoDB driver which dumped my databases/collections in Azure Storage. The issue of the documents never getting created was because DocumentDB doesn't store itself in the Azure Storage resource. When you create a new DocumentDB look under Settings > Collections > Browse to see that no databases exist. Unfortunately it doesn't appear DocumentDB has a desktop db management application and this needs to be done on the azure portal.

So the issue was, plainly, that the database/collection I was pointing to didn't exist for the above reason.

Await Never Returning-

Apparently if you're going to await something you need to await all the way through the call stack.

(async) POST:Api (awaits) -> (async) Service:CreateDocument -> (async) Repository:Set (awaits) -> (async) DocumentClient:CreateDocumentAsync

The problem was that even though my service was async, it was not awaiting the call to Repository:Set. Awaiting everything up the chain allowed me to step-through and receive a response from the server.