Visual Studio keeps overwriting NewtonSoft.Json.DL

2019-01-21 22:09发布

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.

8条回答
叼着烟拽天下
2楼-- · 2019-01-21 22:48

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.

查看更多
太酷不给撩
3楼-- · 2019-01-21 22:53

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”).

查看更多
疯言疯语
4楼-- · 2019-01-21 22:59

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.

查看更多
ら.Afraid
5楼-- · 2019-01-21 23:01

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.

查看更多
Bombasti
6楼-- · 2019-01-21 23:03

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

查看更多
成全新的幸福
7楼-- · 2019-01-21 23:05

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.

查看更多
登录 后发表回答