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

拟墨画扇 提交于 2019-12-05 08:05:31

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.

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>

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!