我正在调用我的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;
}
}
Run Code Online (Sandbox Code Playgroud)
是否有可能代码挂起,因为我试图在同一个USING语句中查询和插入文档.
对我来说,创建一个新的客户端实例并创建一个单独的块来处理文件INSERT是一个更好的主意吗?
小智 6
如果对async方法的调用挂起,通常是因为通过使用.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)**;
Run Code Online (Sandbox Code Playgroud)
有关ConfigureAwait的更多详细信息
| 归档时间: |
|
| 查看次数: |
2488 次 |
| 最近记录: |