6

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?

Community
  • 1
  • 1
Pickle
  • 215
  • 2
  • 6
  • 13
  • 1
    Are you sure that version of the dll is part of your deployment package? Do you have it set to copy local? – Maess Dec 05 '13 at 16:30
  • Yes,I made sure that dll was part of deployment package. – Pickle Dec 05 '13 at 16:54
  • And is it in the bin on the server? – Maess Dec 05 '13 at 16:57
  • Yes,its in bin folder. – Pickle Dec 05 '13 at 17:09
  • 1
    +1: Just had this problem and found (eventually) it was down to having a second DLL reference to NewtonSoft.Json in the same csproj file! Details in answer below. Might have been caused by previous NuGet update (or a bad source control merge, as I have the project on 3 PCs). – iCollect.it Ltd Jan 23 '14 at 12:49

5 Answers5

6

Have you tried putting an assembly redirect in your web config to make sure your application is looking for the correct version:

    <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed"  culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="4.5.0.0" />
  </dependentAssembly>
James
  • 2,195
  • 1
  • 19
  • 22
6

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.

iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202
2

Manage Nuget packages for the entire solution, not just the project. You should see multiple versions of Newtonsoft.Json there. Pick the lowest version and then choose Manage. Uncheck all the selected checkboxes and confirm. After it has been successfully removed, repeat the process for any other lesser versions. When all you have left is one, latest, version of the package, click Manage on this one and check any projects where it's missing. Once it's done installing, you'll be good to go.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
0

The latest version of Newtonsoft JSON is 5.0.8, which is most likely what your reinstalling via NuGet got you.

If when you right click and look at the properties for the reference to Newtonsoft JSON dll and it says 5.0.8, you can just set Specific Version to False and your current code should work.

Scott Wylie
  • 4,725
  • 2
  • 36
  • 48
  • My code works fine on my local instance and when I deploy it on Azure website I still keep getting "Could not load file or assembly Newtonsoft.Json" error. – Pickle Dec 06 '13 at 18:50
  • And as I hinted at, is the version that you deployed in your bin folder, 4.5 or is 5.0.8? – Scott Wylie Dec 06 '13 at 18:56
  • Yes, its been deployed to bin folder and the version is 5.0.8. Matter of fact I have taken a Azure technical support with Microsoft. If I get any response from them I will certainly post the solution here. – Pickle Dec 06 '13 at 22:29
  • 1
    So you have deployed 5.0.8.0 but your code is looking for 4.5.0.0 and your bindingRedirect says newVersion is 4.5.0.0. I would suggest doing a find all on the entire solution for '4.5.0.0 and update them to '5.0.8.0' and see what happens. – Scott Wylie Dec 06 '13 at 22:49
0

The Project Template in VS2013 has this really annoying habit, it references Newtonsoft.Json as a nuget package HOWEVER under the reference properties "Copy Local" it is marked as False.

Mark "Copy Local" to True

Essentially create new project -> deploy doesn't work out the box without this tweak.

Side Note: Upgrading Newtonsoft.Json will change the Copy Local value to True.

Oliver
  • 35,233
  • 12
  • 66
  • 78