Reputation: 365
I have two solutions Sln1 and Sln2, Sln1 have two projects (there are more but, this situation is used for simplicity) projectA and ProjectB. ProjectA is WebApi and ProjectB is Class library project. Both of this project use Newtonsoft.Json 10.0.0. In another Sln2 I have ProjectC and added ProjectB from Sln1. In ProjectC I also have Newtonsoft.Json but with version 6.0.0., when I add ProjectB as reference project to ProjectC I got this error:
Could not load file or assembly 'Newtonsoft.Json 10.0.0., Culture=neutral' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference.
Here is how my solutions are organized.
-Sln1
--ProjectA - WebApi - Newtonsoft.Json 10.0.0
--ProjectB - Class library - Newtonsoft.Json 10.0.0
-Sln2
--ProjectC - WebApp - Newtonsoft.Json 6.0.0
--ProjectB - Class library (add from Sln1) - Newtonsoft.Json 10.0.0
This situation not working, also note ProjectB is added as reference in ProjectA and ProjectC.
But when I downgrade Newtonsoft.Json version in ProjectB from 10.0.0 to 6.0.0, I didn't get error both of my solution compiled and works in runtime.
-Sln1
--ProjectA - WebApi - Newtonsoft.Json 10.0.0
--ProjectB - Class library - Newtonsoft.Json 6.0.0
-Sln2
--ProjectC - WebApp - Newtonsoft.Json 6.0.0
--ProjectB - Class library (add from Sln1) - Newtonsoft.Json 6.0.0
Why is this case, I thought I need to have same version of NuGet packet in both solutions? If this is situation which works can I always rely on this approach, so I don't need to have assemblies with same versions among other projects?
Upvotes: 0
Views: 60
Reputation: 2327
Add binding redirect configuration for Newtonsoft.Json in the configuration file will solve the problem.
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="10.0.0.0" />
</dependentAssembly>
</assemblyBinding>
You can also refer to this page.
Upvotes: 1