C# - System.Runtime.InteropServices.RuntimeInformation 对 MongoDB 的持续困扰

标签 c# mongodb

我们有一个由一堆小工具组成的项目。

他们所有人都使用 MongoDB,并且没有一个没有一次或多次因为该错误而惹恼我:

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.



他们都有一个 app.config 文件,我什至不知道其来源,内容如下:
<?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>


同时,NuGet 有以下一行:
  <package id="System.Runtime.InteropServices.RuntimeInformation" version="4.3.0" targetFramework="net462" />

所以,很明显,我有 Interop lib 版本 4.3.0,但有些文件我不知道想要版本 4.0.1.0(请注意,甚至没有相同的位数)。
这通常是通过删除库,重新添加它来解决的,然后......很快,同样的问题会再次出现,通常是在一些 NuGet 更新等之后。

它似乎只发生在我们拥有 MongoDB 库的项目上,其中 NuGet 中的版本号与创建 app.config 文件的任何内容不同步。

最佳答案

在具有面向 .NET Standard 2.0 的库版本的情况下,这些绑定(bind)重定向是必需的。 .NET 4.6.2 最初不支持 .NET Standard 2.0(它在 .NET Standard 2.0 之前发布),因此需要额外的 DLL 才能在 .NET 4.6.2 上启用 .NET Standard 2.0 支持,并绑定(bind)重定向这些DLL需要。
如果您能够以 .NET 4.7.2 为目标,则不需要这些绑定(bind)重定向。此外,检查您的 Visual Studio 版本(否则将添加更多 dll 以支持具有 .net 版本的旧 Visual Studio)。
如果您无法更新您的 .NET 版本,请尝试设置正确的 bindingRedirect ,而不是从 web.config 中删除此部分。

<bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.3.0.0" />
这意味着使用 4.0.1.0 编译的库将在运行时使用 4.3.0.0 版本。

关于C# - System.Runtime.InteropServices.RuntimeInformation 对 MongoDB 的持续困扰,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44825245/

相关文章:

mongodb - 按日期间隔分组

IEnumerable 与 List 上的 C# foreach - 元素修改仅对数组持久 - 为什么?

c# - 是否可以编写可以测试 AuthorizationPolicy 对象的测试?

c# - 在多宿主 Windows 10 计算机上接收 UDP 多播消息

node.js - MongoDB:对 2 个集合的原子调用(查找+插入)

mongodb - 如何在mongodb shell中查看分片键?

node.js - nodejs和mongoskin,保存所有项目后的回调

c# - 对称密码算法之间的区别

c# - SortedDictionary.ValueCollection 的枚举器行为不同于其他枚举器

php - 使用php在mongoDB中创建数据库