HttpClientHandler throwing PlatformNotSupportedExc

2019-05-02 19:16发布

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?

2条回答
Lonely孤独者°
2楼-- · 2019-05-02 19:32

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.

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.

查看更多
该账号已被封号
3楼-- · 2019-05-02 19:54

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.

查看更多
登录 后发表回答