.NET Core 配置 - System.Net connectionManagement/maxconnections?

mho*_*ger 5 migration configuration system.net .net-core

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

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

<system.net>
    <connectionManagement>
      <add address="*" maxconnection="65535"/>
    </connectionManagement>
</system.net>
Run Code Online (Sandbox Code Playgroud)

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

谢谢。

小智 11

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

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

如果要限制 HttpClient 连接,请使用HttpClientHandler.MaxConnectionsPerServer


Sco*_*aig 1

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

事情会是这样的:

.UseKestrel(options =>
{
    options.Limits.MaxConcurrentConnections = 100;
})
Run Code Online (Sandbox Code Playgroud)

您还可以将其添加到 HttpClientHandler 中,它称为 MaxConnectionsPerServer。可以在这里看到。

  • 您可以将其添加到 HttpClientHandler 中,是的。它称为 MaxConnectionsPerServer。[链接](https://github.com/dotnet/corefx/blob/master/src/System.Net.Http/ref/System.Net.Http.cs#L83) (2认同)