C# - System.Runtime.InteropServices.RuntimeInformation constant annoyance with MongoDB

9

We have a project made of a bunch of small tools.

All of them use MongoDB and there hasn't been one of them that hasn't annoyed me at one time or another with that error:

System.IO.FileNotFoundException occurred HResult=0x80070002
Message=Could not load file or assembly 'System.Runtime.InteropServices.RuntimeInformation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.

and all of them have an app.config file that I don't even know the origin of with the following content:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <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>
</assemblyBinding>

At the same time, NuGet has the following line:

  <package id="System.Runtime.InteropServices.RuntimeInformation" version="4.3.0" targetFramework="net462" />

So, obviously, I have the Interop lib version 4.3.0, but some file I have no clue about wants version 4.0.1.0 (note that there is not even the same number of digits). This is usually fixed by removing the lib, re-adding it and... soon, again, the same problem will come once more, usually after some NuGet updates, etc.

It seems to happen only on the projects where we have the MongoDB libs where the version number in NuGet gets out of sync with whatever creates the app.config file.

c#
mongodb
asked on Stack Overflow Jun 29, 2017 by Thomas

1 Answer

0

These binding redirects are necessary in those cases which has a version of the library targeting .NET Standard 2.0. .NET 4.6.2 didn't originally support .NET Standard 2.0 (it was released before .NET Standard 2.0), so there are additional DLLs that are needed to enable .NET Standard 2.0 support on .NET 4.6.2, and binding redirects are needed for those DLLs.

If you are able to target .NET 4.7.2, then you won't need these binding redirects. Also, check your visual studio version (otherwise some more dlls will get added to support old visual studio with .net version).

If you are not able to update your .NET version, then try to set proper bindingRedirect, instead of deleting this section from web.config.

<bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.3.0.0" />

This means that the library, which was compiled with 4.0.1.0 will use version 4.3.0.0 at runtime.

answered on Stack Overflow Apr 19, 2021 by KushalSeth

User contributions licensed under CC BY-SA 3.0