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?

Computer
  • 2,149
  • 7
  • 34
  • 71
  • 1
    https://stackoverflow.com/questions/49307481/supporting-multiple-versions-of-nuget-package-in-one-library-nuget-package does this answer it – Jawad Dec 09 '19 at 13:40
  • 1
    Consolidated versions of one library/nuget package is really bad thing... Try to aviod it any possible way. You can try separate your libraries on two assembiles with separated binding reditects (first assembly to v9.0.1.0 and second to v6.0.0.0). But i don't guarantee it works. – picolino Dec 09 '19 at 13:41
  • Does this answer your question? [Newtonsoft.json assembly package version mismatch](https://stackoverflow.com/questions/17236342/newtonsoft-json-assembly-package-version-mismatch) – Lynn Crumbling Dec 09 '19 at 13:44
  • These are two dlls one residing under bin and the second under a folder within the bin directory. I am not using NuGet – Computer Dec 09 '19 at 13:49
  • I don't think Nuget is related to your problem. As in that question, you are trying to load two versions of the same assembly into the same AppDomain. A BindingRedirect is the correct solution, which every upvoted answer on that question called out. – Lynn Crumbling Dec 09 '19 at 13:52
  • I tried adding - but this doesnt work. Changed to version 9.0.1.0 and again that doesnt work either – Computer Dec 09 '19 at 13:55
  • 1
    The version number needs to match the assembly that is actually deployed. Check the version number of it in the bin folder – Stuart Dec 09 '19 at 13:59

2 Answers2

1

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>
Stuart
  • 5,358
  • 19
  • 28
  • So how did the link i provided above was giving me the impression this is possible to do? – Computer Dec 09 '19 at 13:44
  • Ok, it is technically possible via externing, but I wouldn't even entertain the idea, it is not something you should use to solve a diamond dependency problem. – Stuart Dec 09 '19 at 13:45
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.

Ygalbel
  • 5,214
  • 1
  • 24
  • 32