【发布时间】:2014-11-22 22:57:50
【问题描述】:
我正在调用我的 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 是否更好?
【问题讨论】:
-
我在这里没有看到任何令人瞠目结舌的危险信号。重用客户端应该更快——它避免了与数据库服务器再次握手。你能给我一个关于这个时序配置文件的想法吗?初始查询需要多长时间?创建文档需要多长时间?
-
发布这个问题后,我决定简化我的逻辑,所以我只做了 INSERT 部分。我仍然超时。请参阅此帖子:stackoverflow.com/questions/27086097/… 如何获取时序配置文件?你的意思是,在我的代码中放置一些 DateTime 变量来捕获代码执行每一行的确切时间,还是有一个工具可以用来捕获时序配置文件?还知道为什么 DocumentDB 在 CreateDocumentAsync() 之后没有返回响应吗?
-
在你的调用栈中,是否有你从非异步方法调用异步方法的地方?
标签: azure-cosmosdb