Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpClient does not use ServicePointManager service points

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?

like image 588
D.R. Avatar asked Sep 03 '18 12:09

D.R.


People also ask

Does HttpClient use ServicePointManager?

HttpClient does not use ServicePointManager service points.

What is ServicePointManager?

ServicePointManager is a static class that manages the creation and destruction of ServicePoint instances. The ServicePointManager creates a ServicePoint when the application requests an Internet resource that is not in the collection of existing ServicePoint instances.


1 Answers

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

like image 195
D.R. Avatar answered Sep 28 '22 07:09

D.R.