.NET Visual Studio 使用来自 NuGet 的引用导致无法加载文件或程序集

.NET Visual Studio using reference from NuGet causes Could not load file or assembly

我在使用 NuGet 时遇到引用(库)依赖性问题,这让我很烦,请找到我的项目及其关系如下:

我决定 Utils 项目应该使用 Newtonsoft.Json 的最低要求版本,即 6.0.4 用于 .NET 2.0 我认为消费者可以自动覆盖它但是我错了

Project AProject BProject C 将针对不同的 .NET 运行时使用不同版本的 Newtonsoft.Json。现在,每当程序进入 Utils 内的代码以使用 Newtonsoft.Json 的任何引用时,它都会抛出异常 Could not load file or assembly 'Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies.

有没有人知道 Google 中的关键字应该是什么或者更好的解决方案?感谢您的时间和关注。

我花了几个小时终于解决了这个问题。

诀窍是将以下块添加到 app.config/web.config 中,使程序将引用从旧版本库重定向到新版本。 (其中值10.0.0.0应该是较新库的版本号)

<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed"
            culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="10.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>