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

时间:2017-10-06 15:31:03

标签: c# asp.net-core

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

ASP.NET 4.6

编写的WebAPI1

ASP.NET Core 2.0

编写的WebAPI2

当我通过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 个答案:

答案 0 :(得分:0)

当我在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?

在我的情况下,堆栈跟踪中的TaskCanceledException设置为false,因此它在HttpClient中超时

new HttpClient { Timeout = TimeSpan.FromMinutes(2) };

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