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

Rev*_*ven 6 c# asp.net-core

我在两个 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)
Run Code Online (Sandbox Code Playgroud)

Rev*_*ven 2

当我在 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()
Run Code Online (Sandbox Code Playgroud)

然而我注意到异常总是在 2 分钟后抛出......

我尝试将 20GB 文件作为控制器方法的一部分(来自 WebAPI1)发送并在那里捕获TaskCanceledException。当出现此异常时,我们可以通过检查标志来判断是否存在超时问题,IsCancellationRequested如果是false,则表示是超时问题。这里有帮助的帖子: HttpClient - 任务被取消?

在我的例子中TaskCanceledException, stacktrace 设置为false,所以 HttpClient 超时

new HttpClient { Timeout = TimeSpan.FromMinutes(2) };
Run Code Online (Sandbox Code Playgroud)

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