Search code examples
c#json.netvisual-studio-2019webapi

Conflict Newtonsoft.Json 6.0.0.0 and 12.0.0.0 in Web Application with Web Api, C#


I have started a new Web Application in Visual Studio 2019. I have added a Web Api. Shortly after or at the same time (can't remember), I startet getting warnings like this:

Warning     No way to resolve conflict between "Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed" and "Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed". Choosing "Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed" arbitrarily.

I noticed but did not do anything as I decided to wait until it gave me problems.

And that is now...

I am about to use a JSon object but when writing this:

JObject jObject = JObject.Parse(result);

The compiler gives me this error:

Error   CS0433  The type 'JObject' exists in both 'Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' and 'Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'

I have searched the web and my conclusions are as follows:

  • It looks like it is Microsoft.AspNet.WebApi.Client that is the only package installed, that has a dependency on Newtonsoft.Json (>= 6.0.4)
  • I have installed Newtonsoft.Json (12.0.3)
  • In Solution Explorer. In reference list, I have only 1 reference. And that is the one for 12.0.3.
  • In Web.Config I have this entry (and only this for Newtonsoft.Json).
    • <dependentAssembly><assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30AD4FE6B2A6AEED" culture="neutral"/><bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0"/></dependentAssembly>
  • In my .csproj I can see 3 different references in 3 different ItemGroups:
    • One for v. 12.0.0.0
    • One for v. 12.0.2
    • And one for v. 12.0.3
  • I tried to uninstall package Newtonsoft.Json, but is not allowed as other packages are dependent on this.
  • I have runned this command: Update-Package Newtonsoft.Json in the Package Manager Console, which told me, that no new updates were available.

And now, I have runned out of ideas. I am stucked. Anyone knows what to do?

****New update**** I found the 6.0.0.0. Please have a look at the picture:

Here in resources I double clicked the Newtonsoft.Json reference and in the object browser I can see both 6.0.0.0 and 12.0.0.0.

Here in resources I double clicked the Newtonsoft.Json reference and in the object browser I can see both 6.0.0.0 and 12.0.0.0.

As you can see, both 6.0.0.0 and 12.0.0.0 are present. 6.0.0.0 is somewhere on my C:-drive inside a SDK.

I have tried to delete the file. My project got nuts. Then I replaced that file with the 12.0.0.0 from my project. This gave me the error that assemplies did not match.

I finally copied the old .dll back. And is a bit stucked again.

Any ideas?


Solution

  • Okay, I fixed it.

    I had 2 references in my .csproj file to Newtonsoft.Json. One pointed at packages......\xxx.12.0.3 and one pointing at packages......\xxx.12.0.2.

    Both of them was above 6.0.0.0 which was the error I got. So I ignored them.

    But acutally, in my project I had only 12.0.3 folder and no 12.0.2 folder. So the reference did not work and VS tried to find another place to look.

    I guess it looks around, maybe using the GAC but finally it ended up deep down my local drive in one Azure SDK and found Newtonsoft.Json 6.0.0.0. And that was the problem.

    I removed the 12.0.2 reference from my .csproj and finally it worked.