c# - WebApi 中的异步方法导致 IIS ExecuteRequestHandler 挂起

标签 c# iis asynchronous asp.net-web-api

我的 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 工作列表中挂起:

enter image description here

如果我从方法中删除异步/等待代码,一切正常。我正在执行哪种异步操作也无关紧要。只要我使用 await SomeMethod()ExecuteRequestHandler 就会挂起。可能是什么原因?

最佳答案

最可能的原因是某些运行时未在 .NET 4.5(或更新版本)中运行。

请确保:

  1. 您的应用程序面向 .NET 4.5 或更新版本。
  2. 您的应用程序在其 web.config 中将属性 httpRuntime.targetFramework 设置为适当的值(例如,4.5)。

关于c# - WebApi 中的异步方法导致 IIS ExecuteRequestHandler 挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33713984/

相关文章:

c# - C# 未初始化的变量危险吗?

c# - EF Core 动态过滤器

url - arr url 重写不适用于外部站点

javascript - AngularJS 的异步特性与摘要/应用

c# - 自定义控件的文本框字符串/文本的填充

c# - 在 UWP C# 中从 PixelBuffer 转换为图像(仅使用 Windows.UI.Xaml)

.net - 将我的 asp.net mvc web 应用程序发布到 IIS 后,无法上传我的内容文件夹中的文件

iis - 如何复制网络场上的内容

spring - 异步添加 Spring bean

javascript - NodeJS mysql同步查询