2014-11-22 98 views
3

我打电话给我的DocumentDB数据库以查询某个人。如果该人不在数据库中,那么我会尝试将该人插入到我的收藏中。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是否更好?

+0

进一步的细节,我不是在这里看到任何令人瞠目的红旗。重新使用客户端应该更快 - 它避免了与数据库服务器进行另一次握手。你能给我一个关于这个时间轮廓的概念吗?最初的查询需要多长时间?文件创建需要多长时间? – 2014-11-23 19:51:31

+0

发布这个问题后,我决定简化我的逻辑,所以我只做了INSERT部分。我仍然超时。看到这篇文章:http://stackoverflow.com/questions/27086097/getting-a-task-was-cancelled-while-trying-to-create-a-document-in-documentdb?noredirect=1#comment42691065_27086097 如何我有时间档吗?你的意思是,在我的代码中加入一些DateTime变量来捕获代码执行每一行的确切时间,或者是否有我可以用来捕获时序配置文件的工具? 还有什么想法为什么DocumentDB在CreateDocumentAsync()之后没有返回响应? – Sam 2014-11-23 20:39:37

+0

在您的调用堆栈中,有没有什么地方可以从非异步方法调用异步方法? – 2017-01-28 17:05:30

回答

2

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


UPDATE:这极有可能被固定在最新的SDK,因为我无法重现它了。

+1

这似乎还在发生,他们现在还没有解决这个问题吗? – Jonathan 2017-09-21 03:12:42

4

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

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

选项2: 你应该在你的异步使用.ConfigureAwait(假)调用DocDB如果要同步调用此方法。试试这个:

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

ConfigureAwait