我打电话给我的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是否更好?
进一步的细节,我不是在这里看到任何令人瞠目的红旗。重新使用客户端应该更快 - 它避免了与数据库服务器进行另一次握手。你能给我一个关于这个时间轮廓的概念吗?最初的查询需要多长时间?文件创建需要多长时间? – 2014-11-23 19:51:31
发布这个问题后,我决定简化我的逻辑,所以我只做了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
在您的调用堆栈中,有没有什么地方可以从非异步方法调用异步方法? – 2017-01-28 17:05:30