嗯,我的问题是,在 Azure 存储表上调用 await table.ExecuteAsync(...) 确实会插入请求的数据,但永远不会完成(不返回 TableResult)。与 InsertOrUpdate 和 Update 操作相同的情况。我还尝试了具有不同数量属性的不同表 - 同样的问题。
当我调用table.Execute(...)时 - 每种操作都运行良好。
这是我的代码 - 就这么简单:
外部调用(在异步操作中放置在 MVC Controller 中):
List<Task<ServiceResult<Boolean?>>> addPostTasks = new List<Task<Common.ServiceResult<bool?>>>();
foreach (var userStream in userStreams)
{
Task<ServiceResult<Boolean?>> addPostTask = postsStorageSvc.AddImagePost(...);
postsAddImagePostTasks.Add(addPostTask);
}
Task.WaitAll(addPostTasks.ToArray());
调用的方法:
public async Task<ServiceResult<Boolean?>> AddImagePost(...)
{
ServiceResult<Boolean?> result = new ServiceResult<bool?>(null);
try
{
PostTableEntity newPost = new PostTableEntity(streamId.ToString(), Guid.NewGuid().ToString(), creatorId, date, htmlText);
TableOperation insertOperation = TableOperation.Insert(newPost);
//Following line never ends!
TableResult tableResult = await this._storageTableBootstrapper.Table.ExecuteAsync(insertOperation);
//Following line works perfect - but is not ASYNC
TableResult tableResult = this._storageTableBootstrapper.Table.Execute(insertOperation);
}
catch (Exception ex)
{
result.Result = false;
result.Errors.Add("AzurePostsStorageService Unexpected error: " + ex.Message);
}
return result;
}
最佳答案
问题出在这里:
Task.WaitAll(addPostTasks.ToArray());
您的异步方法尝试将自身编码(marshal)回 ASP.NET 同步上下文,该上下文由于您使用 Task.WaitAll
发起了阻塞调用而被卡住。
相反,您需要遵循始终异步模式并使用Task.WhenAll
,并使用await
:
await Task.WhenAll(addPostTasks.ToArray);
Stephan Cleary 在他的博文中详细阐述了这一点(@NedStoyanov 添加):
One other important point: an ASP.NET request context is not tied to a specific thread (like the UI context is), but it does only allow one thread in at a time. This interesting aspect is not officially documented anywhere AFAIK, but it is mentioned in my MSDN article about SynchronizationContext.
关于c# - Azure 存储表 : await table. ExecuteAsync(InsertOperation) 执行,但从未完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28189692/