12

Similar to this question, which was marked as a duplicate (so I couldn't answer it directly).

I have a webapi project, which references a PCL project. I was getting this DefaultContractResolver error intermittently.

I had tried all of the troubleshooting steps from that other question, largely involving uninstalling and reinstalling the nuget packages. Didn't help.

Community
  • 1
  • 1
dbruning
  • 5,042
  • 5
  • 34
  • 35

2 Answers2

39

The root cause of the problem for me was that both my webapi project and my PCL project were referencing Newtonsoft.Json.dll. There seemed to be some kind of race condition in Visual Studio build, so that if the PCL project was built last, it would copy its portable version of Newtonsoft.Json.dll into my webapi project's \bin directory.

The solution was simply to go into my PCL project's References, and turn off "Copy local" for Newtonsoft.Json.dll

dbruning
  • 5,042
  • 5
  • 34
  • 35
  • 1
    Thank you so much! I was about to throw my computer out the window – Robert Ros Nov 19 '15 at 15:23
  • OMG - this makes total sense and is not something I would have thought of for quite some time! Thanks dbruning - totally saved me from wasting tons of time tonight! – Paul Schroeder May 02 '17 at 02:46
  • Finally something that works, thanks man; I lost track of how many hours i lost with the install/unistall nuget packages approach. – Ferox May 18 '17 at 08:56
  • Setting "Copy local" to "false" to portable projects worked. – Jamaxack Aug 15 '17 at 06:34
  • stopped me committing Hari Kari today - you star, also sorted Automapper problems too ! Issues with PCL vs full .net 4.5 that have been plaguing me for 2+ years. Can't thank you enough ! – MemeDeveloper Aug 31 '17 at 16:28
3

Two other things that may help:

  1. Removing Temporary ASP.NET files (found in "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files") containing old versions of the NewtonSoft dll.
  2. Removing your NuGet package folder, clearing the NuGet package cache ("Manage NuGet Packages" -> "Settings" -> "NuGet Package Manager" -> "General" -> "Clear Package Cache", then restoring the packages
Paul Siersma
  • 2,036
  • 1
  • 22
  • 25
  • 3
    Thanks. We had a Newtonsoft version issue that we just could not work out because the website had the expected version in it but deleting the files in :C\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files solved it. You sir have helped immensely – Alan Macdonald Nov 20 '19 at 11:10