DocumentDB call hangs

混江龙づ霸主 提交于 2020-02-24 04:35:20

问题


I'm calling my DocumentDB database to query for a person. If the person is not in the database, I'm then trying to insert the person into my collection.

When I check the collection, I see that the new person is being created but my code just seems to hang where I make the second call to insert the person into the collection. Any idea why my code is hanging? I'm not including all the code to save space e.g. GetDatabaseAsync(), GetCollectionAsync(), etc. are all 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'm trying to both query and insert a document inside the same USING statement.

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


回答1:


If a call to an async method hangs it usually is because it being called synchronously by calling it with .Wait() or .Result instead of await ing. You have not shown your calling code, so please include it here.

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

Option 2: You should use .ConfigureAwait(false) in your async calls to DocDB if you are calling this method synchronously. Try this:

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

Further details on ConfigureAwait




回答2:


It seems there is a bug in the client SDK of DocumentDB. Try using client.CreateDocumentAsync(collection.DocumentsLink, user).Wait() instead of await client.CreateDocumentAsync(collection.DocumentsLink, user)


UPDATE: This has most probably been fixed in the latest SDK as I am not able to reproduce it anymore.



来源:https://stackoverflow.com/questions/27083501/documentdb-call-hangs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!