我的 WebApi 2 Controller 中有以下方法
[Route("document/{documentGuid:Guid}/basic")]
[DocumentName("label")]
[HttpGet]
public async Task<HttpResponseMessage> BasicDocument(Guid documentGuid)
{
byte[] data = await _documentsGenerator.CreateDocument(documentGuid);
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new ByteArrayContent(data);
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = string.Format("document-{0}-basic.pdf", documentGuid)
};
return result;
}
当我从浏览器调用它来下载文件时,我得到了文件,但请求仍在 IIS 工作列表中挂起:
如果我从方法中删除异步/等待代码,一切正常。我正在执行哪种异步操作也无关紧要。只要我使用 await SomeMethod()
,ExecuteRequestHandler
就会挂起。可能是什么原因?
最佳答案
最可能的原因是某些运行时未在 .NET 4.5(或更新版本)中运行。
请确保:
- 您的应用程序面向 .NET 4.5 或更新版本。
- 您的应用程序在其
web.config
中将属性httpRuntime.targetFramework
设置为适当的值(例如,4.5
)。
关于c# - WebApi 中的异步方法导致 IIS ExecuteRequestHandler 挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33713984/