DocumentDB调用挂起

时间:2014-11-22 22:57:50

标签: azure-cosmosdb

我正在调用我的DocumentDB数据库来查询某个人。如果此人不在数据库中,我会尝试将此人插入我的收藏中。

当我检查集合时,我看到正在创建新人,但我的代码似乎挂起了我进行第二次调用以将人插入集合的地方。知道我的代码挂起的原因吗?我不包括节省空间的所有代码,例如GetDatabaseAsync(),GetCollectionAsync()等都在工作。

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语句中查询和插入文档。

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

2 个答案:

答案 0 :(得分:6)

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

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

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

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

有关ConfigureAwait

的更多详情

答案 1 :(得分:4)

似乎DocumentDB的客户端SDK中存在错误。尝试使用client.CreateDocumentAsync(collection.DocumentsLink, user).Wait()代替await client.CreateDocumentAsync(collection.DocumentsLink, user)


更新:这很可能已在最新的SDK中修复,因为我无法重现它。

相关问题