如果POST请求中的文件总大小超过30MB,则ASP.NET Core抛出-4077 ECONNRESET

时间:2019-05-28 16:52:30

标签: c# .net-core kestrel-http-server

确切地说,当我尝试将大型文件上传到30MB以上时,我不知道为什么Kestrel会保持关闭连接的原因。

我知道Kestrel在最大请求大小方面的默认设置为30MB,并且我尝试在web.config文件中将最大请求大小增加到100MB:

<security>
  <requestFiltering allowDoubleEscaping="true">
    <!-- This will handle requests up to 100MB -->
    <requestLimits maxAllowedContentLength="104857600" />
  </requestFiltering>
</security>

我也尝试在引导服务器时对其进行配置:

WebHost.UseKestrel(options => options.Limits.MaxRequestBodySize = 104857600) // 100MB

[...]

services.Configure<FormOptions>(options => options.MultipartBodyLengthLimit = 104857600); // 100MB

我尝试在中间件中进行设置:

var maxRequestBodySizeFeature = httpContext.Features.Get<IHttpMaxRequestBodySizeFeature>();
maxRequestBodySizeFeature.MaxRequestBodySize = 104857600;

我尝试了所有可能找到的东西,例如这里的建议:

Increase upload file size in Asp.Net core

在这里:

https://www.talkingdotnet.com/how-to-increase-file-upload-size-asp-net-core/

但是,一旦我的POST请求包含多个一起超过30MB限制的文件,则执行以下行:

foreach (var entry in httpContext.Request.Form /* Exception is thrown by Form property */)
{
}

我收到以下异常:

[Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions.Internal.ConnectionResetException] - Error -4077 ECONNRESET connection reset by peer
   at Microsoft.AspNetCore.Server.Kestrel.Internal.System.IO.Pipelines.PipeCompletion.ThrowFailed()
   at Microsoft.AspNetCore.Server.Kestrel.Internal.System.IO.Pipelines.Pipe.GetResult(ReadResult& result)
   at Microsoft.AspNetCore.Server.Kestrel.Internal.System.IO.Pipelines.Pipe.Microsoft.AspNetCore.Server.Kestrel.Internal.System.IO.Pipelines.IReadableBufferAwaiter.GetResult()
   at Microsoft.AspNetCore.Server.Kestrel.Internal.System.IO.Pipelines.ReadableBufferAwaitable.GetResult()
   at Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal.LibuvOutputConsumer.<WriteOutputAsync>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   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.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.WebUtilities.MultipartReaderStream.<ReadAsync>d__36.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.WebUtilities.StreamHelperExtensions.<DrainAsync>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.WebUtilities.MultipartReader.<ReadNextSectionAsync>d__20.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Http.Features.FormFeature.<InnerReadFormAsync>d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Http.Features.FormFeature.ReadForm()
[...]

据我所知,这不是浏览器关闭连接。在浏览器中,我只选择了一堆文件并上传它们,然后立即在Visual Studio中引发异常。一切都在localhost中完成。

1 个答案:

答案 0 :(得分:0)

我使用以下设置成功提供了11个7mb文件:

初始设置:

WebHost.CreateDefaultBuilder(args)
    .ConfigureKestrel(options =>
    {
        options.Limits.MaxRequestBodySize = 104857600;
    })

Startup.cs中设置MultipartBodyLengthLimit

services.Configure<FormOptions>(options => options.MultipartBodyLengthLimit = 104857600);

RunExtensions.Run中:

Microsoft.AspNetCore.Builder.RunExtensions.Run(app, request =>
{
    if (request.Request.Path.Value == "/test")
    {
        foreach (var file in request.Request.Form.Files)
        {
            // No exceptions
        }
    }
    return Task.CompletedTask;
});