Newtonsoft.Json.dll conflicts between Visual Studi

2019-04-07 13:42发布

I have a asp.net mvc 5 project in VS2013, I updated the nuget packages of everything to latest

then I run into this error

Error   2   The type 'Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver' exists in both 'c:\Program Files (x86)\Microsoft Visual Studio 12.0\Blend\Newtonsoft.Json.dll' and '{path to my project}\packages\Newtonsoft.Json.6.0.1\lib\net45\Newtonsoft.Json.dll'

Does anyone else run into this.

4条回答
再贱就再见
2楼-- · 2019-04-07 14:15

This type of issue mainly occur while using Visual Studio community. the same issue you wont find on Visual Studio professional.

I had this problem because, I was using profession community.

It occur because, while compiling project, then it will takes the reference from both locations one is from 1). "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Blend\Newtonsoft.Json.dll"

2). And other is from your solution/project reference. in this case both versions are different

and it gives

The type 'Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver' exists in both 'c:\Program Files (x86)\Microsoft Visual Studio 12.0\Blend\Newtonsoft.Json.dll' and '{path to my project}\packages\Newtonsoft.Json.6.0.1\lib\net45\Newtonsoft.Json.dll

The only way to get project to compile is to rename the NewtonSoft dll in the Blend folder.

查看更多
叛逆
3楼-- · 2019-04-07 14:26

I got that error because I had an additional

<ItemGroup>
  <Reference Include="Newtonsoft.Json">
    <HintPath>..\packages\Newtonsoft.Json.5.0.6\lib\net45\Newtonsoft.Json.dll</HintPath>
  </Reference>
  <Reference Include="Owin">
    <HintPath>..\packages\Owin.1.0\lib\net40\Owin.dll</HintPath>
    <Private>True</Private>
  </Reference>
</ItemGroup>

in my Web.csproj directly below the usual references which already contained the correct reference to the latest Newtonsoft.Json.dll. In VS only the reference to the newer assembly was shown.

Solution was: I moved the valid Owin reference to the main references ItemGroup and deleted the outdated Newtonsoft.Json.dll reference (manually editing the csproj file).

Cause of the error message: On my machine the HintPath of the old Newtonsoft.Json.dll did not exist, therefor MSBuild was looking elsewhere and took the Blend version.

(BTW: To see why and where MSBuild is looking for a certain assembly use TOOLS -> Options -> Projects and Solutions -> Build and Run -> und set the "MSBuild project build output verbosity" setting to "Detailed" and rebuild the project.)

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-04-07 14:28

I don't use Blend at all so I renamed the Blend Newtonsoft.Json.dll to "Newtonsoft.Json.dll.bak". This was mentioned as a work around in the Connect bug by Marcus. Marcus continues, saying that Blend may not use Newtonsoft.Json.dll much, so even if you use Blend it's possible that this fix will not affect your work.

查看更多
SAY GOODBYE
5楼-- · 2019-04-07 14:36

The root cause is in @blueling's answer, which I will quote:

Cause of the error message: On my machine the HintPath of the old Newtonsoft.Json.dll did not exist, therefor MSBuild was looking elsewhere and took the Blend version.

I just wanted to put this as a separate answer to make it easier to find since people may not read his/her whole answer.

查看更多
登录 后发表回答