Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web API project won't run when its deployed - Could not load file or assembly 'Newtonsoft.Json, Version=4.5.0.0

I keep getting this error when I deploy my MVC 5 WEB API project:

Could not load file or assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.

I have followed this and re-install the NuGet package "Update-Package Newtonsoft.Json -Reinstall" but it didn't work.

Does anyone have any idea here as to what could be going wrong?

like image 982
Pickle Avatar asked Dec 05 '13 16:12

Pickle


1 Answers

Check the reference to the Newtonsoft.json DLL and make sure it has not automatically chosen a version outside of the packages folder in your solution.

My VS 2013 kept finding other copies in various /program files (x86)/... folders. It was ignoring even an explicit add of the package version DLL to the project. Had to dig deeper to find out why this was happening...

Investigations

I opened the project .csproj file, in text edit mode, and searched for all references to Newtonsoft.Json.

It turned out I had not one, but two reference to the DLL in my csproj file (I did not know that was possible). One referenced an older Newtonsoft.json 5.0.6 (current was 5.0.8).

Solution

Rather than manually remove one of them, I added this missing element to the second DLL inclusion. I also changed the version number to 5.0.8:

  <Private>True</Private>

so it now looked like:

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

Private is the setting that defines "Copy Local" for a DLL reference. It then started including the DLL in the output again!

I will figure out which to remove from the csproj file, but for now this may get you going if you have this problem. It looks like the cause may have been a past NUGET update.

like image 155
Gone Coding Avatar answered Sep 22 '22 10:09

Gone Coding