.NET 核心配置 - System.Net connectionManagement/maxconnections?

.NET Core Configuration - System.Net connectionManagement/maxconnections?

我正在将控制台应用程序(REST 客户端应用程序)从 .NET Framework 迁移到 .NET Core。

在我当前的(框架)版本中,我使用 app.config 文件来设置 System.Net 配置:

<system.net>
    <connectionManagement>
      <add address="*" maxconnection="65535"/>
    </connectionManagement>
</system.net>

在 .NET Core 中,我必须使用 JSON 文件进行配置。没有关于使用新配置架构实施这些设置的文档。有谁知道这在新的 JSON 配置中看起来如何,或者在 Core 中实现它的正确方法?我是否需要专门构建一个指定的 "System.Net.json" 配置文件(与 AppSettings.json 分开)?

谢谢。

假设您使用 Kestrel 作为 Web 服务器(而不是通过 IIS 实现),您应该能够在 BuildWebHost 的 UseKestrel 中进行设置。

它会是这样的:

.UseKestrel(options =>
{
    options.Limits.MaxConcurrentConnections = 100;
})

您也可以在 HttpClientHandler 中添加它,它称为 MaxConnectionsPerServer。可以看出here.

我假设您正在尝试避免每个端点 2 个连接的限制,这是 .NET Framework 上的默认设置。 .NET Core 上不存在这样的 限制。所以你根本不需要上面的设置。

请注意,为了获得更好的性能,我们建议在 .NET Core 上使用 HttpClient/HttpClientHandler 而不是 HttpWebRequest/ServicePoint。 HttpWebRequest/ServicePoint API 是 compat-only

如果你想限制HttpClient连接,那么使用HttpClientHandler.MaxConnectionsPerServer

Karel Zikmund 回答的一些补充。 (因为我没有评论权限)

根据此文档,自 .net core 2.0 以来,连接受到限制: https://docs.microsoft.com/en-us/dotnet/api/system.net.servicepointmanager.defaultconnectionlimit?view=netcore-3.1

文档中遗漏的是 ServicePointManager 是否用于 .net 核心 HttpClient 实现。根据此信息,它用于 .net 核心,但用于 HttpWebRequest,而不是 HttpClient:https://github.com/dotnet/runtime/issues/26048