Different versions of Newtonsoft gives an error

0

Im trying to run two versions of Newtonsoft.Json.dll within my site as im using two different plug-ins requiring different versions of Newtonsoft.Json.dll version 6 and version 9.

Came across this article Two different versions of Newtonsoft.Json.dll needed in ASP.NET MVC which is the same problem (but for Win forms if that makes any difference). So my web.config file looks like

  <dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
    <codeBase version="6.0.0.0" href="Newtonsoft.Json.dll" />
    <codeBase version="9.0.1.0" href="dlls\9.0.0.0\Newtonsoft.Json.dll" />
  </dependentAssembly> 

I know version 6 of NSJson works as everything was working until i added the second plugin. If i look at the dll's properties, under the Details tab i see File and Product version as 9.0.1.19813.

I then created a folder under the bin directory called dlls\9.0.0.0\Placed dll in here. If the version is listed as version="9.0.0.0" i get the error

Could not load file or assembly 'Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.

If i change the version to version="9.0.1.0" then i get

Could not load file or assembly 'Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

Which "seems" to imply that im closer to the solution rather than setting the version as 9.0.0.0 but i dont know how to see what manifest definitions its looking for or what it should be set to? Of course i could be totally wrong and wonder how i go about resolving this issue?

c#
dll
json.net
assembly-binding-redirect
asked on Stack Overflow Dec 9, 2019 by Computer

2 Answers

0

Unfortunately what you are trying to do is not possible with the CLR (technically it is with extern aliasing, but no!), you can only have one version of an assembly loaded within an AppDomain (here your app).

This is where "Binding Redirects" come in, you'll need your config to tell the runtime that whenever it tries to load Newtonsoft.Json, Version=6.0.0.0, ... to use version 9.0.0.0 or whatever version.

The binding redirect config looks like this:

<dependentAssembly>
  <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30AD4FE6B2A6AEED" culture="neutral"/>
  <bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0"/>
</dependentAssembly>
answered on Stack Overflow Dec 9, 2019 by Stuart • edited Dec 9, 2019 by Stuart
0

You have to use assembly binding redirect. Your code will be like this:

<dependentAssembly>
<assemblyIdentity name="C"  
                  publicKeyToken="32ab4ba45e0a69a1"  
                  culture="en-us" />  

<bindingRedirect oldVersion="1.1.1.0" newVersion="1.1.2.5" />  
</dependentAssembly>

More information you can find here.

answered on Stack Overflow Dec 9, 2019 by Ygalbel

User contributions licensed under CC BY-SA 3.0