gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 03:09:11 28 4
gpt4 key购买 nike

我在两个 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)

最佳答案

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

   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 文件作为 Controller 方法的一部分(来自 WebAPI1)并在那里捕获 TaskCanceledException 。当这个异常发生时,我们可以通过检查标志 IsCancellationRequested 来判断是超时问题,如果它是 false 就意味着它是超时问题。
有用的帖子在这里:
HttpClient - A task was cancelled?

在我的例子中,stacktrace 中的 TaskCanceledException 被设置为 false ,所以它在 HttpClient 中超时
new HttpClient { Timeout = TimeSpan.FromMinutes(2) };

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

关于c# - 请求内容意外结束 Kestrel ASP.NET Core,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46609190/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com