I have the situation where I have a class library being used in a Unity3D project, and which uses Json serialization/deserialization. Since the official NewtonSoft.Json NuGet doesn't work well together with Unity, I am using Json.Net.Unity3D (v. 9.0.0.0), which has worked fine up until now.
Now I'm setting up another project, where one of the main component has a dependency on NewtonSoft.Json (v. 11.0.0.0). This new project also needs to reference the previously mentioned class library. This compiles without problem, but when the class library tries to use the Json functions, it can't find the needed Json library and I get an error.
System.IO.FileLoadException: 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)
File name: 'Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' ---> System.IO.FileLoadException: Could not load file or assembly 'Newtonsoft.Json, Version=11.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)
File name: 'Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'
Since the two Json library versions have differing PublicKeyTokens, the binding redirect won't work. I've been trying unsuccessfully to find a solution to this problem, but would need some input. These are the possible options I've come up with:
Any insight would be much appreciated - I've wasted way too much time on this problem already.
I'm not sure if this would work, but you could try to have the v9 referenced through nuget package, and the v11 referenced manually from dll file that will be "copy always" to output directory plus it should have different file name. This way the output assembly should load two json dlls - v9 and v11.
I ended up deciding that this was not possible with a reasonable amount of work, so I took another approach. I created an interface that provided the same methods from NewtonSoft.Json used by my library (i.e. JsonConvert.Serialize and Deserialize), and then made a wrapper implementing said interface in each project referencing the library. This allowed me to completely remove any references to NewtonSoft.Json from the library, and it only required a small amount of duplicated code in each project.
User contributions licensed under CC BY-SA 3.0