首页 > 解决方案 > 上传到 Azure WebApp 会引发“ConnectionResetException:客户端已断开连接”

问题描述

我正在使用 C# 开发 .NET Core 3.1 webapp。我使用 Blazor ServerSide 作为我的前端。该应用程序由 Azure 托管。在我的页面上,我有一个上传组件。当我上传 1 个文件时,它工作正常。当我上传 2-3 个文件时它仍在工作,但是当我上传更多文件时,我收到此错误:

(Serilog.AspNetCore.RequestLoggingMiddleware) HTTP "POST" "/api/foo/2/bar" responded 500 in 18501.3265 ms
Microsoft.AspNetCore.Connections.ConnectionResetException: The client has disconnected
 ---> System.Runtime.InteropServices.COMException (0x800704CD): An operation was attempted on a nonexistent network connection. (0x800704CD)
   --- End of inner exception stack trace ---
   at Microsoft.AspNetCore.Server.IIS.Core.IO.AsyncIOOperation.GetResult(Int16 token)

很可能它与 POST 的文件大小有关。在 Visual Studio 中运行我的 webapp 时,我没有任何问题。

我还在日志中看到了这个警告:

(Microsoft.AspNetCore.Server.IIS.Core.IISHttpServer) Increasing the MaxRequestBodySize conflicts with the max value for IIS limit maxAllowedContentLength. HTTP requests that have a content length greater than maxAllowedContentLength will still be rejected by IIS. You can disable the limit by either removing or setting the maxAllowedContentLength value to a higher limit.

我尝试了许多选项来增加限制:

在我的控制器中:

[Authorize]
[DisableRequestSizeLimit]
public class UploadController : BaseApiController

在 Startup.cs 中:

            // To bypass the file limit:
            services.Configure<FormOptions>(options =>
            {
                options.ValueLengthLimit = int.MaxValue;
                options.MultipartBodyLengthLimit = long.MaxValue; // <-- !!! long.MaxValue
                options.MultipartBoundaryLengthLimit = int.MaxValue;
                options.MultipartHeadersCountLimit = int.MaxValue;
                options.MultipartHeadersLengthLimit = int.MaxValue;
            });
            services.Configure<IISServerOptions>(options =>
            {
                options.MaxRequestBodySize = null;
            });

但仍然没有运气。

我是否缺少另一个设置,或者此错误与上传大小无关?

标签: c#azurefile-uploadasp.net-core-3.1blazor-server-side

解决方案


在 Microsoft 支持的帮助下,我花了数周时间解决了这个问题。
看来问题只出在少数客户身上。
我已将 Azure 中的 WebApp 设置更改为使用 HTTP v2 而不是 HTTP v1。
看起来在从我的浏览器到 Azure 的连接中,一跳就丢弃了 HTTP v2 包。当我重置设置时,问题就消失了。


推荐阅读