Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpClientHandler throwing PlatformNotSupportedException

The following code throws "PlatformNotSupportedException" 'Operation is not supported on this platform"

It is a NET standard library (tried compiling against 1.4 and 2.0) that is referenced by a .NET 4.6.1 project that runs as a web app.

var handler = new HttpClientHandler();
handler.SslProtocols = SslProtocols.Tls12;

Why does Tls12 throw this exception and what is a workaround?

like image 455
rollsch Avatar asked Sep 25 '17 08:09

rollsch


2 Answers

The problem here is that the SslProtocols property doesn't exist on .NET Framework, it only exists in .NET Core.

You can see that in the Docs for HttpClientHandler.

  • .NET Framework
  • .NET Core

In .NET Framework you have to set it via ServicePointManager.SecurityProtocol, i.e.

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

To achieve that in your .NET Standard PCL you'll most-likely have to do cross-compile to netstandard-2.0 and net461 as I'm not sure it still exists in .NET Standard/.NET Core.

edit:

Alternatively, remove it from your PCL and set it globally in your .NET Framework application via the static property above.

like image 90
Tseng Avatar answered Oct 21 '22 02:10

Tseng


This property was supposed to be implemented with .NET 4.7.1 per Microsoft docs and is actually implemented and working on .NET 4.7.2.

like image 7
bkaid Avatar answered Oct 21 '22 03:10

bkaid