HttpClient does not use ServicePointManager service points
Asked Answered
M

1

5

By default HttpClient only uses 2 concurrent connections per host. According to docs I can change that. I don't want to change it on a global level, I just want to change it for the service I'm using. Therefore I wrote the following code:

// Increase connection limit in order to have more concurrent requests to MyService
ServicePointManager.FindServicePoint(myServiceUrl, null).ConnectionLimit = 20;

Unfortunately, this doesn't work. The service (called via HttpClient) still uses only 2 concurrent connections. If I change the code to:

ServicePointManager.DefaultConnectionLimit = 20;

At the same code location, it works. However, I don't want to globally change this setting. How to change it locally only?

Edit: I realized that something is setting the connection limit back to 2. Is there any operation (e.g., instantiating a new WebRequestHandler, instantiating a new HttpClient, ...?) which resets the connection limit?

Modernistic answered 3/9, 2018 at 12:41 Comment(4)
Make sure the scheme, port and hostname of myServiceUrl correspond to the actual service you're calling.Gd
Yeah, they are the same.Modernistic
May the downvoter please elaborate why this is not a programming question and why this question should be closed. Thank you,Modernistic
You might look at this question and its answers: How can I programmatically remove the 2 connection limit in WebClient, but given the lack of context here, I'm not sure if it's relevant. You can also see the source code for ConnectionLimit hereCappadocia
M
9

I found the problem, we ran into the following .NET bug:

https://github.com/Microsoft/dotnet/blob/master/releases/net471/KnownIssues/534719-Networking.ServicePoint.ConnectionLimit%20default%20behavior%20changed.md

Modernistic answered 3/9, 2018 at 15:59 Comment(1)
i hit this bug on net462 (despite the KnownIssues page says it was introduced in net471). one can simply run a test that's mentioned in the KnownIssues link above for the framework they are targeting to verify if the bug exists. in net frameworks (so not netcore, and net), i'm seeing this bug fixed only on net472, net48. hope this helps.Tore

© 2022 - 2024 — McMap. All rights reserved.