默认情况下,HttpClient 每个主机仅使用 2 个并发连接。根据 docs我可以改变这一点。我不想在全局范围内更改它,我只想为我正在使用的服务更改它。因此我写了以下代码:
// Increase connection limit in order to have more concurrent requests to MyService
ServicePointManager.FindServicePoint(myServiceUrl, null).ConnectionLimit = 20;
不幸的是,这行不通。该服务(通过 HttpClient 调用)仍然只使用 2 个并发连接。如果我将代码更改为:
ServicePointManager.DefaultConnectionLimit = 20;
在相同的代码位置,它可以工作。但是,我不想全局更改此设置。如何仅在本地更改它?
编辑:我意识到有些东西将连接限制设置回 2。是否有任何操作(例如,实例化一个新的 WebRequestHandler,实例化一个新的 HttpClient,...?)重置连接限制?
最佳答案
我发现了问题,我们遇到了以下 .NET 错误:
https://github.com/Microsoft/dotnet/blob/master/releases/net471/KnownIssues/534719-Networking.ServicePoint.ConnectionLimit%20default%20behavior%20changed.md
关于c# - HttpClient 不使用 ServicePointManager 服务点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52150004/