DocumentDB呼叫挂起 [英] DocumentDB call hangs

查看:79
本文介绍了DocumentDB呼叫挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调用DocumentDB数据库来查询一个人.如果此人不在数据库中,那么我将尝试将该人插入到我的收藏夹中.

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.

当我检查集合时,我看到正在创建新人员,但是我的代码似乎挂起了,我第二次调用该人员将其插入到集合中.知道为什么我的代码挂起了吗?我没有包括所有节省空间的代码,例如GetDatabaseAsync(),GetCollectionAsync()等都可以正常工作.

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

代码是否可能由于我试图在同一USING语句中查询和插入文档而挂起.

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

对我来说,创建客户端的新实例并创建一个单独的块来处理文档INSERT更好吗?

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?

推荐答案

如果对异步方法的调用挂起,通常是因为通过使用.Wait()或.Result而不是等待调用来同步调用它.您尚未显示呼叫代码,因此请在此处输入.

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.

选项1 : 不要同步调用异步方法.这是正确的方法.

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

选项2 : 如果正在同步调用此方法,则应在对DocDB的异步调用中使用.ConfigureAwait(false).试试这个:

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)**;

有关 ConfigureAwait 的详细信息 ConfigureAwait

这篇关于DocumentDB呼叫挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆