按类型违反的继承安全规则:'System.Net.Http.WebRequestHandler'

Inheritance security rules violated by type: 'System.Net.Http.WebRequestHandler'

我在 MVC 应用程序的 web.config 中有以下程序集引用...

<dependentAssembly>
  <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
  <bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.0.0.0" />
</dependentAssembly>

每次安装或更新任何nuget包时,版本号都会自动更新为4.1.1.0。然而,这完全破坏了我的应用程序。它显示错误...

Inheritance security rules violated by type: 'System.Net.Http.WebRequestHandler'. Derived types must either match the security accessibility of the base type or be less accessible.

我发现了其他问题,建议的解决方案是再次更改版本号。然而,这似乎并不是特别令人满意。一定有一些潜在的问题导致我不得不一直使用这个模块的旧版本。我正试图找到一个合适的解决方案,而不是不断地破解它以使其工作。

有谁知道造成这种情况的真正原因,并且知道使用所有软件包的推荐版本修复它的正确方法吗?

事实证明,解决方案比我预期的要简单得多。我从 web.config 中完全删除了 System.Net.Http 引用,现在它可以正常工作了。

我在尝试实例化 KeyVaultClient 时看到此错误。我能够通过安装 nuget 包 System.Net.Http v4.3.2.

来修复它

您还可以验证您是否 "Skip applying binding redirects" 勾选了您的选项。

如果未选中,

Visual studio 正在更新您的 web.config 文件。

一定是System.Net.Http版本问题。我 运行 我在 VS 2015 中的代码,它引发了异常 然后,我 运行 我在 VS 2017 中的代码,它起作用了。