Yet another System.Runtime.InteropServices error

2019-04-05 12:38发布

Every project we have with MongoDB will, at one point of another, have a problem with the System.Runtime.InteropServices library that doesn't load.

This time the error is interesting:

Interop load exception

The outer exception can't find the version 4.3.0.0 of the lib. But the inner exception can't find version 4.0.0.0

Does anyone have an idea about that?


More information about the problem:

enter image description here

So, NuGet has 4.3.0.0 installed

<?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 confirms that I have 4.3.0.0 installed,

however, app.config which always seem to be out of sync with reality:

  <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>

A line was added about version 4.0.1.0

In the same line.. the csproj is nonsense:

<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>

Since it claims to reference 4.0.1.0 with the path of 4.3.0.0

There is something broken and it is always happening with that exact same lib; not just this project: anywhere I include MongoDB, this lib comes as a dependency and, every time, there are some random problems with it.

When I try to load in manually:

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

it fails as well.

I am finding that System.Runtime.InteropServices.RuntimeInformation.dll does NOT get copied to the build folder, even though it's included in the project.


I found a nasty workaround: if I include MongoDB in the main exe, even if I don't use it, it has a dependency on the Interop lib and this forced the lib to be copied to the build folder and then the subsequent calls work.

3条回答
不美不萌又怎样
2楼-- · 2019-04-05 13:27

The System.Runtime.InteropServices.RuntimeInformation v4.3.0 NuGet does indeed install a DLL with the version 4.1.0.0; although confusing it doesn't seem to create any problems.

Try installing the following NuGets on each project that directly or indirectly uses MongoDB v2.4.4

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

This worked for me.

The only case where this didn't work was an MSTest unit/integration test where MSTest appears to ignore binding redirects (seperate issue - seems to be quite common), as such I created my integration test using a regular console exe.

I'm probably way off, but my own experiences/observations suggested that MongoDB.Driver (v2.4.4) has a dependency on System.Runtime.InteropServices.RuntimeInformation, which is satisified by its NuGet dependency on NETStandardLibrary, but that System.Runtime.InteropServices.RuntimeInformation's dependency on System.Runtime.InteropServices isn't taken care of. That's why it's not enough to just upgrade RuntimeInformation. In my case I have many projects that already have a dependency on NETStandardLibrary (v1.6) and so I couldn't use System.Runtime.InteropServices.RuntimeInformation v4.0.0 if I wanted to as 4.3.0 is already installed and can't be removed. I saw both the exceptions that you saw, at different times, and installing both the NuGet packages as above resolved them.

查看更多
混吃等死
3楼-- · 2019-04-05 13:33

I found a solution to this problem by uninstalling every mongodb nuget library that need the nuget package System.Runtime.InteropServices.RuntimeInformation and reinstalling it. In my case it was mongodb nuget package.

After, I upgraded the System.Runtime.InteropServices.RuntimeInformation to 4.3 and add the following in app.config even it's doesn't make sense :

<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" />

查看更多
Bombasti
4楼-- · 2019-04-05 13:37

Targeting framework 4.7 or 4.7.1 would resolved the problem. This is because there are some conflicts at the side of .net standard 2

查看更多
登录 后发表回答