How to have main project using NewtonSoft.Json with subproject using Json.Net.Unity3D?

0

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:

  1. Preventing the main project's Json library propagating to the referenced project somehow. If I run a test version where the main project doesn't have a Json library at all, the sub project works fine.
  2. Hacking the binding redirect to accept different dll:s for different versions. Doesn't seem to be possible.
  3. Having both versions installed in the main project and used where appropriate. Also seems like a non-starter.

Any insight would be much appreciated - I've wasted way too much time on this problem already.

Here's a small test solution that demonstrates the problem

c#
asp.net
unity3d
json.net
assembly-binding-redirect
asked on Stack Overflow Dec 31, 2018 by Nicklas Forss

2 Answers

0

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.

answered on Stack Overflow Dec 31, 2018 by Miq
0

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.

answered on Stack Overflow Jan 3, 2019 by Nicklas Forss

User contributions licensed under CC BY-SA 3.0