Calling the DocumentDB call

I am calling my DocumentDB for a human query. If the person is not in the database, I try to insert it into my collection.

When I inspect the collection, I see that a new person is being created, but my code seems to be hanging where I make the second call to insert the person into the collection. Any idea why my code is hanging? I am not including all the code to save space, eg. GetDatabaseAsync (), GetCollectionAsync (), etc. Everyone is working.

using (client = new DocumentClient(new Uri(endPointUrl), authorizationKey))
{
   //Get the database
   var database = await GetDatabaseAsync();

   //Get the Document Collection
   var collection = await GetCollectionAsync(database.SelfLink, "People");

   string sqlQuery = "SELECT * FROM People f WHERE f.id = \"" + user.PersonId + "\"";

   dynamic doc = client.CreateDocumentQuery(collection.SelfLink, sqlQuery).AsEnumerable().FirstOrDefault();

   if (doc == null)
   {
      // User is not in the database. Add user to the database
      try
      {
         **// This is where the code is hanging. It creates the user in my collection though!**
         await client.CreateDocumentAsync(collection.DocumentsLink, user);
      }
      catch
      {
         // Handle error
      }
   }
   else
   {
      // User is already in the system.
      user = doc;
   }
}

      

Is it possible that the code hangs because I am trying to both request and insert a document inside the same USING statement.

Is it a better idea for me to create a new client instance and create a separate block to process the INSERT document?

+3


source to share


2 answers


If a call to an asynchronization method hangs, it is usually because it is being called synchronously, calling it with .Wait () or .Result instead of waiting. You have not provided your call code, so please enable it here.

Option 1 : Don't call your async method synchronously. This is the correct approach.

Option 2 : You must use .ConfigureAwait (false) in your asynchronous DocDB calls if you call this method synchronously. Try the following:



var database = await GetDatabaseAsync()**.ConfigureAwait(false)**;
...
var collection = await GetCollectionAsync(database.SelfLink, "People")**.ConfigureAwait(false)**;
...
await client.CreateDocumentAsync(collection.DocumentsLink, user)**.ConfigureAwait(false)**;

      

More on ConfigureAwait

+4


source


There seems to be a bug in the DocumentDB client SDK. Try using client.CreateDocumentAsync(collection.DocumentsLink, user).Wait()

insteadawait client.CreateDocumentAsync(collection.DocumentsLink, user)




UPDATE: This is most likely fixed in the latest SDK as I can't reproduce it anymore.

+3


source







All Articles