【问题标题】:.NET Core Configuration - System.Net connectionManagement/maxconnections?.NET Core 配置 - System.Net connectionManagement/maxconnections?
【发布时间】:2018-03-16 06:42:06
【问题描述】:

我正在将控制台应用程序(REST 客户端应用程序)从 .NET 框架迁移到 .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 分开)来执行此操作?

谢谢。

【问题讨论】:

    标签: configuration migration .net-core system.net


    【解决方案1】:

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

    它会是这样的:

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

    你也可以在你的HttpClientHandler中添加这个,它叫做MaxConnectionsPerServer。可以看到here

    【讨论】:

    • 我的应用程序主要是一个 REST 客户端。我不太确定 BuildWebHost。 (编辑了我的 OP 以提及)
    • 您仍然必须使用 Web 服务器来托管您的请求。您必须指定它使用 Kestrel 或其他服务器(如 IIS 集成)。如果您使用模板构建它(CLI 或 Visual Studio),您应该能够在 Program.cs 中看到它。
    • 事实并非如此。我不确定从哪里(在 system.net.http 中)触发我的 http 请求的服务器,但它没有在我的 Program.cs 中实现。我正在使用 Visual Studio。
    • 我可以在我的 HttpClient 对象上设置这个选项吗?
    • 你可以在你的HttpClientHandler中添加它,是的。它称为 MaxConnectionsPerServer。 link
    【解决方案2】:

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

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

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

    【讨论】:

    • 我很高兴听到这个消息,我曾经因为难以诊断的令人发指的默认设置而失去了 DAYS 天,而 Google-Fu 是答案。
    【解决方案3】:

    对 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-06
      • 1970-01-01
      • 2023-03-28
      • 2022-07-29
      • 2019-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多