本文介绍了HttpClientHandler引发PlatformNotSupportedException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码抛出"PlatformNotSupportedException"此平台不支持该操作"

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

这是一个NET标准库(已针对1.4和2.0进行了尝试),该NET标准库由作为Web应用程序运行的.NET 4.6.1项目引用.

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;

为什么Tls12会引发此异常,什么是解决方法?

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

推荐答案

此处的问题是SslProtocols属性在.NET Framework中不存在,仅在.NET Core中存在.

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

您可以在HttpClientHandler的文档中看到它.

You can see that in the Docs for HttpClientHandler.

  • .NET Framework
  • .NET Core

在.NET Framework中,您必须通过ServicePointManager.SecurityProtocol进行设置,即

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

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

要在.NET Standard PCL中实现该目标,您很有可能必须交叉编译为netstandard-2.0 net461,因为我不确定它是否仍存在于.NET Standard中/.NET Core.

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.

或者,将其从PCL中删除,并通过上述静态属性在.NET Framework应用程序中对其进行全局设置.

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

这篇关于HttpClientHandler引发PlatformNotSupportedException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 20:07