Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ServicePointManager in ASP.NET Core

I'm trying to convert an existing class library code to a .NET Core class library. In that code in a static constructor I have the following:

ServicePointManager.DefaultConnectionLimit = 100;
ServicePointManager.Expect100Continue = false;

I did some searching and ServicePointManager is no longer available in .NET Core, and WinHttpHandler should be used now (ServicePointManager.DefaultConnectionLimit in .net core?).

My question is what exactly is the ServicePointManager and what are those properties that are being set?

The WinHttpHandler is not static as ServicePointManager so I'd have to create an instance to set those properties? do I need to change all my http calls to make use of that instance?

like image 430
developer82 Avatar asked Sep 18 '16 16:09

developer82


People also ask

What is the ServicePointManager?

ServicePointManager is a static class used to create, maintain, and delete instances of the ServicePoint class.

What is the use of ServicePointManager SecurityProtocol?

Setting ServicePointManager. SecurityProtocol to TLS 1.2 affects on whole Application Domain and breaks all the other requests and network - based APIs from application that don't support TSL 1.2. Sometimes theese APIs use default settings and your code breaks them.

What is ServicePointManager DefaultConnectionLimit?

The DefaultConnectionLimit property sets the default maximum number of concurrent connections that the ServicePointManager object assigns to the ConnectionLimit property when creating ServicePoint objects.

Does HttpClient use ServicePointManager?

HttpClient connections management in . NET Framework 4.5 and +, the default constructor of HttpClient uses the HttpClientHandler class, which leverages the HttpWebRequest class to send HTTP requests. Therefore, we can use the good old ServicePoint and ServicePointManager classes to manage opened connections.


1 Answers

WinHttpHandler inherits from HttpMessageHandler, so you can pass it as a parameter when constructing you HttpClient like follows:

WinHttpHandler httpHandler = new WinHttpHandler();
httpHandler.SslProtocols = SslProtocols.Tls12;

HttpClient client = new HttpClient(httpHandler);

Hope this helps!

like image 171
Juan Alvarez Avatar answered Sep 24 '22 20:09

Juan Alvarez