Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio keeps overwriting NewtonSoft.Json.DLL with an older version

Visual Studio is overwriting the correct version of NewtonSoft.Json.DLL that I have configured in both my project references and the NuGet package file with an older version when I build any other project besides the website that contains the reference.

OK. Here is the scenario:

I have a solution with a backend service and a website. The website is running on .NET 4.5 and is configured with NuGet to pull in version 6.0.1 of Newtonsoft.Json.DLL.

<package id="Newtonsoft.Json" version="6.0.1" targetFramework="net45" /> 

Which adds the dependenAssembly binding to the web.config file.

  <dependentAssembly>     <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />     <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />   </dependentAssembly> 

I can build and run this website without any problems.

I recently updated all of the class libraries and backend service from .NET 4.0 to .NET 4.5. After the update, whenever I build one of the class libraries or run/debug the backend service, the website becomes inoperable.

Could not load file or assembly 'Newtonsoft.Json' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) 

I tracked this down to the fact that when rebuilding one of the class libraries or running/debugging the backend service from Visual Studio, the Newtonsoft.Json.DLL gets overwritten with an older version of the file - version 4.5.11. Because of the explicit dependentAssembly binding, any time I access the website after that I get the 'Could not load ...' error mentioned above.

This would be OK if I just wanted to run one or the other of the backend service or the website, but I have to run them both together to get my application running properly. But because of this error I cannot have the backend service running at the same time as the website or the website crashes.

How do I prevent Visual Studio from overwriting the DLL?

Note that I have the reference set for only 6.0.1 across the entire solution (i.e. there is no reference anywhere to 4.5.11). And in the website I have 'Copy Local' set to true and 'Specific Version' is also set to true for the Newtonsoft.Json.DLL.

like image 392
lecklind Avatar asked Mar 18 '14 21:03

lecklind


People also ask

What is Newtonsoft JSON DLL used for?

The Newtonsoft. JSON namespace provides classes that are used to implement the core services of the framework. It provides methods for converting between . NET types and JSON types.

How do I find Newtonsoft JSON DLL version?

I would check the version of Newtonsoft. Json in the bin directory of the start up project. If Newtonsoft. Json is there and it is the version you are expecting, then you can use Fuslogvw (https://msdn.microsoft.com/en-us/library/e74a18c4(v=vs.110).aspx) to see where the loader is trying to get the 4.0.

What is Newtonsoft JSON NuGet?

NuGet packages (37.0K)This package adds support for formatting and content negotiation to System. Net. Http. It includes support for JSON, XML, and form URL encoded data.


2 Answers

This is a known bug in Windows Azure VS Tools

Workarounds:

  • Remove Newtonsoft.Json.dll file from Program Files\Microsoft SDKs\Windows Azure.NET SDK\v2.3\ref\ folder.

  • Uninstall Windows Azure VS Tools v 2.3

like image 148
Alexander Puchkov Avatar answered Sep 23 '22 12:09

Alexander Puchkov


The Problem

Your csproj contains a reference with an invalid path to the Newtonsoft.Json dll. In my case, it was

<HintPath>..\..\packages\Newtonsoft.Json\lib\net45\Newtonsoft.Json.dll</HintPath>

instead of the one NuGet should have set, packages\Newtonsoft.Json.8.0.3\... (incl. version number).

Since VS cannot find the dll, it will just search on your system, and use the first one it finds. On my system, that was Azure SDK 2.9, then Azure SDK 2.8, then VS12/Blend/....

The Solution

Some of the solutions above (deleting all Newtonsoft.Json.dlls you find in your system) might hide the problem in the short-term, but only fixing the csproj to point to the correct NuGet-supplied path will really solve the issue.

That is, make sure the HintPath in your csproj corresponds to the package path where the NuGet package is installed.

If you have bash, you can use

$ grep -r HintPath * | grep Newtonsoft

in the root directory of your solution to find the offending csproj.

Related errors

If you have this problem, starting your Asp.Net site with the explicit redirect in web.config might fail with an exception page, with the following text in the error message:

LOG: Attempting download of new URL newtonsoft json

WRN: Comparing the assembly name resulted in the mismatch: Major Version

Even if some projects have a reference to the NuGet of Newtonsoft.Json 8.x, VS will happily compile, then overwrite that DLL with the ancient one that it found on the system, and fail at runtime.

like image 21
Wilbert Avatar answered Sep 23 '22 12:09

Wilbert