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

42

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.

c#
visual-studio-2012
dll
visual-studio-2013
.net-4.5
asked on Stack Overflow Mar 18, 2014 by lecklind

9 Answers

28

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

answered on Stack Overflow Nov 11, 2014 by Alexander Puchkov
10

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.

answered on Stack Overflow Jun 10, 2016 by Wilbert • edited Jun 20, 2020 by Community
5

Here is the situation I had.
3 projects in solution. Projects A and B have referenced Newtonsoft.Json.DLL 6.0.3 and a Solution Reference to project C. Project C has no any explicit reference to Newtonsoft.Json.DLL.
When building the solution it builds C, then A and B - dropping correct dll's in bin. But when i build only C VS drops older version of dll to A and B. As no explicit reference or binding Redirect exists, it is taking it from GAC. Also Building only A drops older dll into B, because it builds C at first drops wrong version into A and B, then builds A putting the correct version.

Here is the solution - explicitly add Newtonsoft.Json.DLL 6.0.3 to the project C

answered on Stack Overflow Jul 12, 2014 by nsb • edited Jul 15, 2014 by nsb
4

We recently ran into the same issue. Our solution would compile and have the correct DLLs on our development machines, but on our build agent the wrong version of Newtonsoft.Json would be dropped in the output folders.

After a lot of time invested, we discovered that this was triggered by someone installing a newer version of the Azure SDK on our build agent than we had locally: 2.9 instead of 2.5.1.

The workaround we discovered was to include the Newtonsoft.Json NuGet package in every project in the solution, even if the project had no need for the reference.

answered on Stack Overflow Nov 5, 2016 by StilgarISCA
3

I have exact the same problem and was finding out that in C:\Program Files\Microsoft SDKs\Windows Azure.NET SDK\v2.3\ref I have Newtonsoft.Json.dll with the exact same date and time as the one that whas copied into my website folder.

After rename/delete the Newtonsoft.Json.dll in C:\Program Files\Microsoft SDKs\Windows Azure.NET SDK\v2.3\ref, Visual Studio was stopping to replacing my referenced version and the website start working again.

answered on Stack Overflow Apr 17, 2014 by Patric Forsgard
3

My scenario was almost exactly same except that my Newtonsoft.JSON DLL was copied from different location. I verified that my solution was referencing the correct file and version but on RUN VS copied it from another location (Check this first by dragging BIN DLL into VS or properties.

In end after trying them one by one using Fusion logs I went all out replacing all references of Newtonsoft.JSON.dll that has the same incorrect version from Program files:

  • 'C:\Program Files (x86)\Microsoft SDKs\Microsoft Azure\Mobile Services\1.0'
  • 'C:\Program Files\Common Files\Microsoft Shared\Visual Studio\12.0'
  • 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\Blend'
  • etc.

Quick tip: In explorer under details view add 'Product Version' as column and sort by it: enter image description here

It still feels like crappy IDE behavior through if I set Specific Version for that Assembly and the file path is to the correct DLL (set by NuGet) it should really not go and override it with one from another share global location. Any comments to change Visual Studio build behavior here will be appreciated as I really don't want to do this type of manual hacks on each developers machine.

enter image description here

answered on Stack Overflow Apr 11, 2016 by Marius Vorster
2

I ran into this same sort of problem today. I discovered that after a build of a class library, all the *.dll files in that class library’s output directory are copied into the bin folder of any web project that has a project reference to that class library. This can lead to compatible assemblies getting replaced with incompatible assemblies. However, this dll xcopy won’t happen while the web project is unloaded (right-click the project and choose “Unload Project”).

answered on Stack Overflow May 17, 2014 by erli
0

I have the same problem, after test all solutions I continue to get errors. Appear to be that this error can happen for multiple causes.

In my case I use VS 2015, the problem was a unused reference to other project in my application that use a older version of newtonSoft. I eliminate the reference and the dll was no more changed.

answered on Stack Overflow Mar 8, 2017 by freedeveloper
0

I came with same problem .

I had installed newtonsoft 11.0 version with nuget.

After building or publishing the solution the newtonsoft reference in bin folder was getting overwritten to 6.0 version and the published pages dll had the same 6.0 version. Everything was fine (package.config,debug,release refernces).

After lot of tracing found the solution that worked for me.

So tried with following .

  1. Renamed the newtonsoft.dll file in the Diagnostic folder at following location Program Files\Microsoft SDKs\Azure.NET SDK\v2.8\bin\plugins\Diagnostics

It started working as expected.

answered on Stack Overflow May 9, 2021 by Nilesh Agnihotri

User contributions licensed under CC BY-SA 3.0