Azure Cosmos DB 中的 ExecuteNextAsync freezes/hangs
ExecuteNextAsync freezes/hangs in Azure Cosmos DB
创建新的 cosmos DB 时,Azure 中会显示一些代码示例。大多数代码都有效。但是我有这个单一的功能,在执行时只是 freezes/hangs 。有什么想法可以解决或排除故障吗?
代码:
public async Task<IEnumerable<T>> GetItemsAsync(Expression<Func<T, bool>> predicate, string collection)
{
IDocumentQuery<T> query = Client.CreateDocumentQuery<T>(
UriFactory.CreateDocumentCollectionUri(DatabaseId, collection),
new FeedOptions { MaxItemCount = -1, EnableCrossPartitionQuery = true })
.Where(predicate)
.AsDocumentQuery();
List<T> results = new List<T>();
while (query.HasMoreResults)
{
try
{
var result = await query.ExecuteNextAsync<T>();
results.AddRange(result);
}
catch(Exception ex)
{
}
}
return results;
}
调用函数:
return await ItemsRepo.GetItemsAsync(p=>p.RunId == runId, "files-and-folders-with-unique-permissions");
最后 windows 应用:
var items = _db.GetFilesAndFoldersWithUniquePermissions(txtRunId.Text).Result;
完整代码:
存储库:
public async Task<IEnumerable<Item>> GetFilesAndFoldersWithUniquePermissions(string runId)
{
var collectionLink = UriFactory.CreateDocumentCollectionUri("kk-governance-reporting", "files-and-folders-with-unique-permissions");
return await ItemsRepo.GetItemsAsync(p=>p.RunId == runId, "files-and-folders-with-unique-permissions");
}
应用程序:
private void BtnGenerateReport_Click(object sender, EventArgs e)
{
var items = _db.GetFilesAndFoldersWithUniquePermissions(txtRunId.Text).Result;
string json = JsonConvert.SerializeObject(items.ToArray());
json = JToken.Parse(json).ToString(Formatting.Indented);
//write string to file
System.IO.File.WriteAllText(@"c:\temp\unique-files-and-folders.txt", json);
MessageBox.Show("Completed");
}
混合使用 async-await 和 .Result
阻塞调用会导致死锁。
在这种情况下,事件处理程序允许 async void
。
引用Async/Await - Best Practices in Asynchronous Programming
所以我建议你在事件处理程序中重构 App 以等待
private async void BtnGenerateReport_Click(object sender, EventArgs e) {
var items = await _db.GetFilesAndFoldersWithUniquePermissions(txtRunId.Text); //<--
string json = JsonConvert.SerializeObject(items.ToArray());
json = JToken.Parse(json).ToString(Formatting.Indented);
//write string to file
System.IO.File.WriteAllText(@"c:\temp\unique-files-and-folders.txt", json);
MessageBox.Show("Completed");
}
让代码按预期运行。
创建新的 cosmos DB 时,Azure 中会显示一些代码示例。大多数代码都有效。但是我有这个单一的功能,在执行时只是 freezes/hangs 。有什么想法可以解决或排除故障吗?
代码:
public async Task<IEnumerable<T>> GetItemsAsync(Expression<Func<T, bool>> predicate, string collection)
{
IDocumentQuery<T> query = Client.CreateDocumentQuery<T>(
UriFactory.CreateDocumentCollectionUri(DatabaseId, collection),
new FeedOptions { MaxItemCount = -1, EnableCrossPartitionQuery = true })
.Where(predicate)
.AsDocumentQuery();
List<T> results = new List<T>();
while (query.HasMoreResults)
{
try
{
var result = await query.ExecuteNextAsync<T>();
results.AddRange(result);
}
catch(Exception ex)
{
}
}
return results;
}
调用函数:
return await ItemsRepo.GetItemsAsync(p=>p.RunId == runId, "files-and-folders-with-unique-permissions");
最后 windows 应用:
var items = _db.GetFilesAndFoldersWithUniquePermissions(txtRunId.Text).Result;
完整代码:
存储库:
public async Task<IEnumerable<Item>> GetFilesAndFoldersWithUniquePermissions(string runId)
{
var collectionLink = UriFactory.CreateDocumentCollectionUri("kk-governance-reporting", "files-and-folders-with-unique-permissions");
return await ItemsRepo.GetItemsAsync(p=>p.RunId == runId, "files-and-folders-with-unique-permissions");
}
应用程序:
private void BtnGenerateReport_Click(object sender, EventArgs e)
{
var items = _db.GetFilesAndFoldersWithUniquePermissions(txtRunId.Text).Result;
string json = JsonConvert.SerializeObject(items.ToArray());
json = JToken.Parse(json).ToString(Formatting.Indented);
//write string to file
System.IO.File.WriteAllText(@"c:\temp\unique-files-and-folders.txt", json);
MessageBox.Show("Completed");
}
混合使用 async-await 和 .Result
阻塞调用会导致死锁。
在这种情况下,事件处理程序允许 async void
。
引用Async/Await - Best Practices in Asynchronous Programming
所以我建议你在事件处理程序中重构 App 以等待
private async void BtnGenerateReport_Click(object sender, EventArgs e) {
var items = await _db.GetFilesAndFoldersWithUniquePermissions(txtRunId.Text); //<--
string json = JsonConvert.SerializeObject(items.ToArray());
json = JToken.Parse(json).ToString(Formatting.Indented);
//write string to file
System.IO.File.WriteAllText(@"c:\temp\unique-files-and-folders.txt", json);
MessageBox.Show("Completed");
}
让代码按预期运行。