19

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?

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
developer82
  • 13,237
  • 21
  • 88
  • 153

3 Answers3

12

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!

Mani Gandham
  • 7,688
  • 1
  • 51
  • 60
Juan Alvarez
  • 307
  • 2
  • 5
7

ServicePointManager is available in .NET Core from v2.0

Assembly System.Net.ServicePoint, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51

That said, I have implemented my HTTP Client calls using:

System.Net.Http.HttpClientHandler handler;  // use DI etc to create 
if (options.SkipSsl)  // config driven
    handler.ServerCertificateCustomValidationCallback = (req, cer, ch, err) => true;

See https://stackoverflow.com/a/44540071/4292717

Peter
  • 971
  • 11
  • 12
0

Just add the following namespaces:

using System.Net.Http;

using System.Net.Http.Headers;

then change your line as below,

WinHttpHandler httpHandler = new 
WinHttpHandler();

httpHandler.SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls;

instead of

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | 
SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

Happy Coding !!!

CKE
  • 1,533
  • 19
  • 18
  • 29