45

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.

lecklind
  • 471
  • 1
  • 4
  • 5
  • 1
    I'd recommend you to enable verbose logging for build and track the exact source of "wrong" version of the dll. – alex.b Mar 18 '14 at 21:44
  • Exactly the same problem! I Found myself in the situation after NUGET updating OWIN,IDENTITY and EF. While correctly building one project, VS2013 drops an older version of Newtonsoft.Json to other projects, older version is the same as in GAC. – nsb Jul 11 '14 at 23:09

9 Answers9

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

Alexander Puchkov
  • 5,913
  • 4
  • 34
  • 48
  • 5
    Same Problem with v2.8. I had to rename the DLL in the folder C:\Program Files\Microsoft SDKs\Azure\.NET SDK\v2.8\bin\plugins\Diagnostics – Srini Dec 28 '16 at 22:21
  • 1
    For 2.9 the dll is located in \Program Files\Microsoft SDKs\Azure\.NET SDK\v2.9\ToolsRef. – Jorn Theunissen Jan 03 '18 at 14:12
  • 1
    Had the same issue with v2.9 with this file C:\Program Files\Microsoft SDKs\Azure\.NET SDK\v2.9\bin\plugins\Diagnostics\Newtonsoft.Json.dll – thmsn Jan 24 '19 at 19:41
  • Your "known bug" link is now broken : ( Do you have some kind of ID to the bug? – John Dyer Sep 29 '20 at 12:18
  • @JohnDyer ticket id in the link is 850425 and title was "windows azure vs tools breaking msbuild for web projects". I could not find this ticket anymore after Microsoft restructured connect website. – Alexander Puchkov Oct 01 '20 at 00:45
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.

Community
  • 1
  • 1
Wilbert
  • 7,251
  • 6
  • 51
  • 91
  • 1
    I was skeptical that this was the solution since I was using NuGet to manage my references, and other projects were working correctly. But when I opened up the project's .csproj in a text editor, sure enough, there was the correct reference to JSON.net and then below, in a separate , a reference to an old 5.x version of the DLL which was missing. Deleting the reference via the VS2015 GUI did not remove this reference. I had to use the text editor. I recompiled and the JSON.NET .DLL is finally the correct version. – Matt Jan 01 '17 at 20:17
  • NuGet stores the different versions of the DLL's for each of the different .NET target frameworks. For me, I deleted the old .NET target directories then deleted the file from my bin directory and the problem resolved itself. – Adam Aug 10 '18 at 19:58
  • 1
    If you *don't* have `bash` and `grep`, you can use `findstr /s /c:HintPath *.csproj | findstr /c:Newtonsoft`. – David Mar 22 '19 at 17:30
  • This solved my problem as well. In my case I had two references to Newtonsoft.Json - one valid, one invalid. I deleted the invalid path and all is well. – barrett777 Jul 01 '19 at 15:35
  • Note that this also happens if a library you're referencing references Newtonsoft.JSON, but your project doesn't explicitly reference any version. MSBuild will warn about unresolved dependencies, and then find 6.0.8 in the SDK and copy that into your bin folder. – Zhaph - Ben Duguid Nov 29 '19 at 12:15
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

nsb
  • 414
  • 4
  • 11
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.

StilgarISCA
  • 158
  • 9
  • After digging into our build logs (enabling detailed build logs) we found an "unresolved reference" for Newtonsoft.JSON from other libraries in our projects, which caused MSBuild to find the version from the SDK. Adding the explicit version reference resolved this copy. – Zhaph - Ben Duguid Nov 29 '19 at 12:25
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.

  • Searching for dll with the exact same date worked for me. C:\Program Files\dotnet\sdk\NuGetFallbackFolder\newtonsoft.json\11.0.2\lib\net45 was in fact containing 10.0.3 dll – Gabriel GM Jan 10 '19 at 21:00
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

Marius Vorster
  • 196
  • 1
  • 10
  • Something to remember with NuGet and changing versions of a package: a) **Let NuGet do the version change** (Do not uninstall a package version to install a new one, select the new version and it will update the old version) b) **Let NuGet resolve dependencies** (Try to update on the highest level i.e. Solution and with parent package) only if this does not work update the dependent package versions. If you do the above and update the bindingRedirect (i.e. For all versions from oldVersion="0.0.0.0" use newVersion="7.0.0.1") then only exceptions will remain. – Marius Vorster Aug 08 '16 at 12:54
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”).

erli
  • 131
  • 1
  • 4
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.

freedeveloper
  • 3,670
  • 34
  • 39
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.