Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WebApi中的异步方法导致IIS ExecuteRequestHandler挂起_C#_Iis_Asynchronous_Asp.net Web Api - Fatal编程技术网

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

C# WebApi中的异步方法导致IIS ExecuteRequestHandler挂起,c#,iis,asynchronous,asp.net-web-api,C#,Iis,Asynchronous,Asp.net Web Api,在我的WebApi 2控制器中有以下方法 [Route("document/{documentGuid:Guid}/basic")] [DocumentName("label")] [HttpGet] public async Task<HttpResponseMessage> BasicDocument(Guid documentGuid) { byte[] data = await _documentsGenerator.CreateDocument(documentGu

在我的WebApi 2控制器中有以下方法

[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;
}
[路由(“document/{documentGuid:Guid}/basic”)]
[文件名称(“标签”)]
[HttpGet]
公共异步任务基本文档(Guid文档Guid)
{
字节[]数据=等待_documentsGenerator.CreateDocument(documentGuid);
HttpResponseMessage结果=新的HttpResponseMessage(HttpStatusCode.OK);
结果.内容=新的ByteArrayContent(数据);
result.Content.Headers.ContentType=新的MediaTypeHeaderValue(“应用程序/八位字节流”);
result.Content.Headers.ContentDisposition=新的ContentDispositionHeaderValue(“附件”)
{
FileName=string.Format(“document-{0}-basic.pdf”,documentGuid)
};
返回结果;
}
当我从浏览器调用它下载文件时,我得到了该文件,但请求仍在IIS工作人员列表中挂起:


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

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

请确保:

  • 您的应用程序的目标是.NET 4.5或更新版本
  • 您的应用程序在其
    web.config
    中的属性
    httpRuntime.targetFramework
    设置为适当的值(例如
    4.5

  • 看看是的,我的目标是.NET4.5,并且在Web.config中有targetFramework属性。但是,它可能与IIS或Windows有关,因为此错误发生在Windows 8.1和VS 2013上,而在Windows 10和VS 2015上,此问题不会发生