又一个System.Runtime.InteropServices错误

时间:2017-07-01 00:50:31

标签: c# .net windows-runtime interop

我们与MongoDB合作的每个项目都会出现无法加载的System.Runtime.InteropServices库的问题。

这次错误很有趣:

Interop load exception

外部异常无法找到lib的4.3.0.0版本。 但内部异常无法找到版本4.0.0.0

有人对此有所了解吗?

有关此问题的更多信息:

enter image description here

所以,NuGet已经安装了4.3.0.0

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="System.Runtime" version="4.3.0" targetFramework="net462" />
  <package id="System.Runtime.InteropServices.RuntimeInformation" version="4.3.0" targetFramework="net462" />
</packages>

packages.config 确认我已安装4.3.0.0,

然而, app.config 似乎总是与现实不同步:

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

添加了关于版本4.0.1.0

的行

在同一行...... csproj是胡说八道:

<Reference Include="System.Runtime.InteropServices.RuntimeInformation, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
  <HintPath>x:\Packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll</HintPath>
</Reference>

因为它声称引用4.0.1.0的路径为4.3.0.0

有一些东西被破坏了,它总是发生在那个完全相同的lib上;不仅仅是这个项目:在我包含MongoDB的任何地方,这个lib都是一个依赖项,每次都有一些随机问题。

当我尝试手动加载时:

        var Name = new AssemblyName("System.Runtime.InteropServices.RuntimeInformation, Version=4.3.0.0");
        var Asm = Assembly.Load(Name);

它也失败了。

我发现System.Runtime.InteropServices.RuntimeInformation.dll不会被复制到build文件夹,即使它已包含在项目中。

我发现了一个讨厌的解决方法: 如果我在主exe中包含MongoDB,即使我不使用它,它依赖于Interop lib,这迫使lib被复制到build文件夹,然后后续调用工作。

3 个答案:

答案 0 :(得分:3)

System.Runtime.InteropServices.RuntimeInformation v4.3.0 NuGet确实安装了4.1.0.0版本的DLL;虽然令人困惑但似乎没有造成任何问题。

尝试在直接或间接使用MongoDB v2.4.4的每个项目上安装以下NuGets

  • System.Runtime.InteropServices.RuntimeInformation v4.3.0
  • System.Runtime.InteropServices v4.3.0

这对我有用。

这不起作用的唯一情况是MSTest单元/集成测试,其中MSTest似乎忽略绑定重定向(单独的问题 - 似乎很常见),因此我使用常规控制台exe创建了我的集成测试。

我可能已经离开了,但是我自己的经验/观察结果表明MongoDB.Driver(v2.4.4)依赖于System.Runtime.InteropServices.RuntimeInformation,这是因为它对NETStandardLibrary的NuGet依赖性令人满意,但是System.Runtime.InteropServices.RuntimeInformation对System.Runtime.InteropServices的依赖性没有得到解决。这就是为什么只升级RuntimeInformation是不够的。在我的情况下,我有许多项目已经依赖于NETStandardLibrary(v1.6),因此我无法使用System.Runtime.InteropServices.RuntimeInformation v4.0.0,如果我想已经安装了4.3.0并且可以'被删除。我在不同的时间看到了你看到的两个异常,并且如上所述同时安装了两个NuGet包解决了它们。

答案 1 :(得分:0)

通过卸载需要nuget包System.Runtime.InteropServices.RuntimeInformation的每个mongodb nuget库并重新安装它,我找到了解决这个问题的方法。就我而言,它是mongodb nuget包。

之后,我将System.Runtime.InteropServices.RuntimeInformation升级到4.3并在app.config中添加以下内容,即使它没有意义:

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

答案 2 :(得分:0)

定位框架4.7或4.7.1将解决此问题。 这是因为.net标准2方面存在一些冲突

相关问题