请求内容意外结束 Kestrel ASP.NET Core

Posted

技术标签:

【中文标题】请求内容意外结束 Kestrel ASP.NET Core【英文标题】:Unexpected end of request content Kestrel ASP.NET Core 【发布时间】:2017-10-06 15:31:03 【问题描述】:

我在两个 WebApi 之间发送大 (20GB) 文件时遇到问题。

WebAPI1 写在ASP.NET 4.6

WebAPI2 写在ASP.NET Core 2.0

当我通过 Postman 将此文件发送到 WebAPI2 时,会发送整个文件。但是,当我尝试将文件从 WebAPI1 发送到 WebAPI2 时,它会失败(尽管我可以发送 7GB 这样的文件)。

在发送 20GB 文件期间,我在 WebAPI2 中收到错误:

Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException: Unexpected
 end of request content.
   at Microsoft.AspNetCore.Server.Kestrel.Internal.System.IO.Pipelines.PipeCompl
etion.ThrowFailed()
   at Microsoft.AspNetCore.Server.Kestrel.Internal.System.IO.Pipelines.Pipe.GetR
esult(ReadResult& result)
   at Microsoft.AspNetCore.Server.Kestrel.Internal.System.IO.Pipelines.Pipe.Micr
osoft.AspNetCore.Server.Kestrel.Internal.System.IO.Pipelines.IReadableBufferAwai
ter.GetResult()
   at Microsoft.AspNetCore.Server.Kestrel.Internal.System.IO.Pipelines.ReadableB
ufferAwaitable.GetResult()
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.MessageBody.<ReadAs
ync>d__22.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNot
ification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.FrameRequestStream.
<ReadAsyncInternal>d__21.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNot
ification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Microsoft.AspNetCore.WebUtilities.BufferedReadStream.<EnsureBufferedAsync>
d__37.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNot
ification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Microsoft.AspNetCore.WebUtilities.MultipartReaderStream.<ReadAsync>d__36.M
oveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNot
ification(Task task)
   at System.IO.Stream.<CopyToAsyncInternal>d__27.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNot
ification(Task task)

【问题讨论】:

【参考方案1】:

当我在 WebApi1 中将此请求作为 WCF 合同的一部分运行时,我得到了堆栈跟踪:

   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at System.Threading.Tasks.Task`1.get_Result()

但是我注意到 Exception 总是在 2 分钟后抛出......

比我尝试发送那个 20GB 的文件作为控制器方法的一部分(来自 WebAPI1)并在那里捕获TaskCanceledException。当这个异常发生时,我们可以通过检查标志IsCancellationRequested来判断是超时问题,如果它是false则意味着它是超时问题。 有用的帖子在这里: HttpClient - A task was cancelled?

在我的情况下,stacktrace 中的 TaskCanceledException 设置为 false,因此在 HttpClient 中超时

new HttpClient  Timeout = TimeSpan.FromMinutes(2) ;

将限制增加到 10 分钟解决了问题。

【讨论】:

以上是关于请求内容意外结束 Kestrel ASP.NET Core的主要内容,如果未能解决你的问题,请参考以下文章

在没有 ASP.NET 核心的情况下使用 Kestrel

IIS充当代理转发请求到Kestrel

ASP.NET Web API,从 Flex FileReference 上传时 MIME 多部分流意外结束

从 ASP.Net 5“空”项目中删除 Kestrel

ASP.NET Core Web服务器 Kestrel和Http.sys 特性详解

结合Jexus + Kestrel 部署 asp.net core 生产环境