首页 > 解决方案 > ASP.NET Core SignalR websocket 连接限制

问题描述

我使用 Microsoft.AspNetCore.SignalR.Client 对托管在 Windows Server 2016 标准上的 SignalR (ASP.NET Core) 应用程序进行负载测试。已安装 Dotnet 核心托管 2.1.1

而且我不能创建超过 3000 (2950-3050) 个连接。

已经尝试过这里描述的建议:

添加限制UseKestrel(如果我将值设置为 100 或 1000,这似乎有效):

var host = new WebHostBuilder()
    .UseKestrel(options =>
    {
        options.Limits.MaxConcurrentConnections = 50000;
        options.Limits.MaxConcurrentUpgradedConnections = 50000;
    })

通过添加以下内容更改了所有 aspnet.config 文件:

<system.web>
    <applicationPool maxConcurrentRequestsPerCPU="50000" />
</system.web>

执行了这个命令:

cd %windir%\System32\inetsrv\ appcmd.exe 设置配置 /section:system.webserver/serverRuntime /appConcurrentRequestLimit:50000

为Web Service\Current Connections - Maximum Connections添加了性能计数器。最大连接数增加到 3300 并停止。

服务器日志中没有异常。但是我觉得系统有一些限制。

服务器 IIS 日志仅包含以下内容:

获取 /messageshub id=A_3x1sH9kHM1Rc3oPSgP6w 80 - 172.20.192.11 - - 404 0 0 3

客户端异常基本上如下:

System.Net.Http.HttpRequestException:将内容复制到流时出错。---> System.IO.IOException: Unable to read data from the transport connection: 一个现有的连接被远程主机强行关闭。

标签: asp.net-coresignalrsignalr.clientasp.net-core-signalr

解决方案


在 Windows 上,您可能会遇到动态端口分配问题。默认情况下,Windows 有 5000 个端口号准备分配给 TCP 连接,其中 1024 个保留给操作系统本身,您最终将有 3977 个端口可以自由分配。在您的情况下,您提到的数字是 3300,但有可能建立了 3300 个连接,其中 677 个是 Time_Waited。无论如何,我建议使用

netstat -an | find 'Established" -c 
netstat -an | find 'TIME" -c 
netstat -an | find 'CLOSED" -c 

为了确定在您收到 IO 异常时已建立 & time_wait & close_wait 连接的数量,如果数量接近 5000,只需将其添加到您的注册表并重新启动并再次测试

  [HKEY_LOCAL_MACHINE \System \CurrentControlSet \Services \Tcpip \Parameters]
MaxUserPort = 5000 (Default = 5000, Max = 65534)

推荐阅读