Pierre Arnaud
Pierre Arnaud

Reputation: 10547

Json.NET initialization throws System.TypeLoadException

I have a solution with two components:

As long as only B uses and refers to Json.NET (as a NuGet package), everything just works fine. But as soon as I add a reference to the very same NuGet package from A, I consistently get:

System.TypeLoadException: Could not load type 'Newtonsoft.Json.SerializationBinder' ...

I tracked down the issue to be caused by NuGet including two different versions of the Json.NET assembly:

Apparently, one assembly cannot be replaced by the other. My PCL no longer finds the version it expects, hence the exception.

How can I configure NuGet so that both projects refer to the same portable version of Json.NET?

Upvotes: 2

Views: 1072

Answers (1)

Pierre Arnaud
Pierre Arnaud

Reputation: 10547

I have found a workaround to make sure that both B and A refer to the same Json.NET Portable assembly.

By default, NuGet configured the <HintPath> to be set to the net45 version of the library:

<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
  <SpecificVersion>False</SpecificVersion>
  <HintPath>..\packages\Newtonsoft.Json.6.0.6\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>

which is at the root of this conflict. So edit the A.csproj file to this instead:

<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
  <SpecificVersion>False</SpecificVersion>
  <HintPath>..\packages\Newtonsoft.Json.6.0.6\lib\portable-net45+wp80+win8+wpa81+aspnetcore50\Newtonsoft.Json.dll</HintPath>
</Reference>

With that in place, the exact same assembly will be used both by A and B.

Upvotes: 4

Related Questions