如何处理需要不同版本程序集的两个库?

时间:2022-10-19 23:28:49

I have a C# ASP.NET site that uses a 3rd party library with a dependency on JSON.NET. It pulls in [Newtonsoft.Json, Version=4.0.5.0] as part of its reference.

我有一个C#ASP.NET站点,它使用了一个依赖于JSON.NET的第三方库。它引入了[Newtonsoft.Json,Version = 4.0.5.0]作为其参考的一部分。

Yesterday, I added a reference to a different 3rd party library with a dependency on the latest version of JSON.NET. I'm now stuck in a situation where I can only get one of these libraries to work at any given time.

昨天,我添加了对不同第三方库的引用,该库依赖于最新版本的JSON.NET。我现在陷入困境,我只能在任何给定的时间让这些库中的一个工作。

If I just drop the new reference in to the project, calls to it fail with:

如果我只是将新引用放入项目中,则对它的调用将失败:

Could not load file or assembly 'Newtonsoft.Json, Version=9.0.0.0

... and if I update the JSON.NET dependency for the original library, the new code works fine but calls to the old library fail with:

...如果我更新原始库的JSON.NET依赖项,新代码工作正常,但调用旧库失败:

Could not load file or assembly 'Newtonsoft.Json, Version=4.0.5.0

I can understand the first failure. Fair enough, you need a newer version. But the second one baffles me. There are no breaking changes between version 4 and 9. It should be able to use the newer .dll just fine.

我能理解第一次失败。很公平,你需要一个更新的版本。但第二个让我感到困惑。版本4和9之间没有重大变化。它应该能够使用较新的.dll就好了。

Regardless, I'm at a point where I'm blocked. I've neutered the new code and removed the references to the new library that causes the trouble. But I don't have a plan for how to proceed.

无论如何,我正处于被阻挡的地步。我已经对新代码进行了绝育,并删除了导致问题的新库的引用。但我没有计划如何进行。

Any ideas?

1 个解决方案

#1


11  

You need an assembly binding redirect. For example, in your web.config file:

您需要一个程序集绑定重定向。例如,在您的web.config文件中:

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

That basically says "If you want to use any version of Newtonsoft.Json earlier than 9, just load v9 instead."

这基本上说“如果你想使用早于9的任何版本的Newtonsoft.Json,只需加载v9。”

This only works because Json.NET hasn't made (many?) breaking changes between versions. With full SemVer, using major version numbers for breaking changes (and embracing that possibility), I suspect we'll see more difficult situations in the future...

这只能起作用,因为Json.NET没有(许多?)在版本之间进行更改。有了完整的SemVer,使用主要版本号来破坏变化(并且拥抱这种可能性),我怀疑我们将来会看到更多困难的情况......

#1


11  

You need an assembly binding redirect. For example, in your web.config file:

您需要一个程序集绑定重定向。例如,在您的web.config文件中:

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

That basically says "If you want to use any version of Newtonsoft.Json earlier than 9, just load v9 instead."

这基本上说“如果你想使用早于9的任何版本的Newtonsoft.Json,只需加载v9。”

This only works because Json.NET hasn't made (many?) breaking changes between versions. With full SemVer, using major version numbers for breaking changes (and embracing that possibility), I suspect we'll see more difficult situations in the future...

这只能起作用,因为Json.NET没有(许多?)在版本之间进行更改。有了完整的SemVer,使用主要版本号来破坏变化(并且拥抱这种可能性),我怀疑我们将来会看到更多困难的情况......