我打電話給我的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