c# - Azure 存储表 : await table. ExecuteAsync(InsertOperation) 执行,但从未完成

标签 c# .net azure async-await azure-table-storage

嗯,我的问题是,在 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/

相关文章:

c# - vs2010/c#中的表命名空间错误

c# - 线程是否在等待锁 FIFO?

c# - UtcNow 到人类/系统友好的字符串

Azure 函数 : PDFInfoNotInstalledError: Unable to get page count. poppler 是否已安装并位于路径中?对于 pdf2image

c# - 如何在 C# 循环中多次更改控件的属性

c# - Entity Framework 中具有虚拟属性的 System.ObjectDisposedException

c# - 未调用 DataTemplate 中的命令

.net - RavenDb - 安全模式下的 CertificateNameMismatchException

.net - 你如何在 vb.net 中解析 HTML

Azure Functions、功能键无法访问并且门户中的文件看起来丢失