json.netportable-class-librarydotnet-httpclienttypeloadexception

System.TypeLoadException: Inheritance security rules violated by type: 'System.Net.Http.Formatting.JsonContractResolver'


I'm getting a very annoying error about System.Net.Http.Formatting.JsonContractResolver having inheritance security rules violated in a project which uses PCLs.

Here is the setup and how I tracked down the problem.

Project 1 - Custom.WebApi (portable, targeting .net 4.5, Core 5.0, Win8, Xamarin.Android, Xamarin.iOS, Xamarin.iOS(Classic))

This project is an abstraction over HttpClient to unify the return types of Get, Post, Delete, ... and depends ONLY on Newtonsoft.Json and .NET with this profile.

We are using Newtsonsoft.Json pcl version 6.0.8.($packagesFolder\Newtonsoft.Json.6.0.8\lib\portable-net40+sl5+wp80+win8+wpa81\Newtonsoft.Json.dll)

Project 2 - Custom.WepApi.Tests (unit test project)

This is a test project for the WebApi project. It references full version of 6.0.8 ($packagesFolder\Newtonsoft.Json.6.0.8\lib\net45\Newtonsoft.Json.dll)

This project is able to run tests against WebApi project no problem.

Project 3 - MyWebsite (ASP.NET WebApi site)

This project is a WebApi project where the response types are unified and found in the Custom.WebApi project. This references a bunch of 3rd party stuff, but also Newtonsoft.Json v 6.0.8 full ($packagesFolder\Newtonsoft.Json.6.0.8\lib\net45\Newtonsoft.Json.dll)

Project 4 - MyWebsite.Tests

To test the web project, I have a test project that calls endpoints using Custom.WebApi implementation. This project also references newtonsoft.json version 6.0.8. However, this references is purely to fix errors observed when running. Nothing in this project depends on Newtonsoft.Json, but if I do not explicitly add a reference to it, it will fail with TypeLoadException. Adding the reference seems to make that go away.

When I run the Project 4 tests, they all fail, indicating that the website is having TypeLoadException on Startup. After a TON of investigation, I noticed that when the MyWebsite project builds, the Newtonsoft.Json.dll in the bin folder is overwritten with the portable version and fails.

If I change the test project to reference the same portable newtonsoft.json.dll, the test project fails with the same error. Using the full version, no error is observed.

If I manually replace the dll in the bin folder of MyWebsite, it is overwritten again upon build. I have made sure the references are correct in MyWebsite .csproj file, including the hint path.

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

So, either I need to resolve why it believes there is an inheritance security rule violation (looking at the source code did not reveal anything obvious about access modifiers not matching), or it needs to stop overwriting the Newtonsoft.Json.dll in the MyWebsite bin folder.

To further complicate things, this replacement only occurs SOMETIMES. If I Start Without Debugging, it seems to not happen all the time and executing the Unit Tests succeeds against the deployment when the dll is not replaced with portable version.


Solution

  • You can resolve this by setting "Copy Local" to false on the Newtonsoft.Json reference in your PCL projects. I'll show you tomorrow.