7

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?

Jason Kester
  • 5,951
  • 9
  • 35
  • 40
  • 1
    Possible duplicate of [Third party components referencing different versions of the same assembly](http://stackoverflow.com/questions/21699706/third-party-components-referencing-different-versions-of-the-same-assembly) –  Jul 13 '16 at 09:32

1 Answers1

14

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

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks for that. In my case I was also running up against the issue where this section will be ignored if there's a namespace on the configuration section (mentioned here: http://stackoverflow.com/a/12011221/150370), but this did fix the problem. – Jason Kester Jul 13 '16 at 13:33