How to deal with two libraries that need different versions of an assembly?

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.

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.

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:

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.

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?

Jon Skeet
people
quotationmark

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

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

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

people

See more on this question at Stackoverflow