How to work with two libraries that need different versions of the assembly?

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

Yesterday I added a link to another third-party library depending on the latest version of JSON.NET. I'm stuck in a situation where I can only get one of these libraries to work at any given time.

If I simply drop the new link into the project, calls to it will not be made with:

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

... and if I update the JSON.NET dependency for the source library, the new code will work fine, but the calls to the old library end with:

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

I can understand the first glitch. Fair enough, you need a newer version. But the second puzzles me. There is no change in the change between versions 4 and 9. It should be able to use the new .dll just fine.

Regardless, I am at the point where I am locked. I castrated the new code and deleted links to the new library, which causes problems. But I have no plan how to proceed.

Any ideas?

+6
source share
1 answer

You need assembly binding redirection . For example, in your 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> 

It basically says: "If you want to use any version of Newtonsoft.Json before 9, just download instead of v9."

This only works because Json.NET did not (many?) Break changes between versions. With the full SemVer, using major version numbers for breaking changes (and embracing this feature), I suspect we'll see more difficult situations in the future ...

+11
source

All Articles