无法加载文件或程序集'System.Runtime.InteropServices.RuntimeInformation

Could not load file or assembly 'System.Runtime.InteropServices.RuntimeInformation

每当我尝试 运行 具有应用程序洞察力和 entity framework 的 webjob 项目时,我都会收到此错误。

System.IO.FileLoadException: 'Could not load file or assembly 'System.Runtime.InteropServices.RuntimeInformation, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)'

我已经安装了以下 nuget 包

Microsoft.Azure.WebJobs.Logging.ApplicationInsights 版本 2.1.0-beta4

Microsoft.Extensions.Logging 版本 2.0.0

Microsoft.Extensions.Logging.主机版本 2.0.0.

这一切都适用于一个新的 Visual studio 2017 webjob 项目,当我尝试包含一个现有的代码库时,主要使用 entity framework 我得到了这个错误。当我查看有效的参考资料时,我没有 System.Runtime.InteropServices.RuntimeInformation,但它已被添加到带有 entity framework 的项目中。它似乎是 .net 标准的一部分,但为什么我的新控制台应用程序不需要 .net 标准!

我不确定为什么它要查找版本 0.0.0.0,因为我的版本是 4.0.2.0

我也试过将它添加到项目文件中,但是没有用。

<PropertyGroup>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
   <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>

如有任何帮助,我们将不胜感激

非常感谢

您的配置文件中是否缺少加载的程序集?确保您的 web.config 中有类似于以下内容的内容。 NuGet 通常会这样做,但也许它没有,并且它不知道要加载什么

<dependentAssembly>
  <assemblyIdentity name="System.Runtime.InteropServices.RuntimeInformation" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
  <bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" />
</dependentAssembly>

确认 dwilliss 上面的评论对我也有用。解决方案是摆脱:

<dependentAssembly>
  <assemblyIdentity name="System.Runtime.InteropServices.RuntimeInformation" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
  <bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" />
</dependentAssembly>

(在我的例子中来自 app.config,用于 Windows 服务。)我的项目仅间接依赖于 System.Runtime.InteropServices.RuntimeInformation。它是我导入的 NuGet 包的依赖项。

如果您已将项目的 .NET 运行时版本从 4.7.1 之前的版本更新到 4.7.1 或更高版本,然后卸载 Nuget 包,删除/注释掉 App.config 部分(如果它仍然存在),并且重新添加框架中的引用。 4.7.1以后的framework里都有,之前要加Nuget包。

[编辑]...根据上面迈克尔的评论,我在记忆中赞成。

当您从其他平台添加项目引用并尝试构建它时,有时也会发生此类错误。删除引用,这应该有效。