简体   繁体   中英

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?

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>

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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM