JamesNK / Newtonsoft.Json

Json.NET is a popular high-performance JSON framework for .NET
https://www.newtonsoft.com/json
MIT License
10.71k stars 3.24k forks source link

AssemblyVersion only contains Major version causes DLL conflicts #2887

Open manuelmainz opened 1 year ago

manuelmainz commented 1 year ago

Environment

I am using Nuget package 13.0.3 and my application has target framework net48.

On a customer machine any other tool has registered an older version (probably 13.0.1) of Newtonsoft.Json in GAC (Global Assembly Cache). Both versions of Newtonsoft have Assembly version 13.0.0.0.

Even though our application does not register Newtonsoft in GAC, we still break because another application, which is out of our control, (not really sure which one it is) and installed on client machine registers in GAC. So you get the complains by the customer, even though it is not even your fault.

In my opinion the Assembly version should always contain the Major and Release version number. In this way the application is able to determine if there is the same version in GAC or not.

Source code

var a = new JsonSerializerSettings();
a.Formatting = Formatting.Indented;

var b = new JsonSerializerSettings(a);

Expected behavior

The assembly Newtonsoft v 13.0.3.0 out of my binary directory should get loaded.

Actual behavior

The assembly gets loaded from GAC and causes following exception to be thrown, because this constructor has been introduced with version 13.0.2. System.MissingMethodException: 'Method not found: 'Void Newtonsoft.Json.JsonSerializerSettings..ctor(Newtonsoft.Json.JsonSerializerSettings)'.'

Steps to reproduce

  1. download Newtonsoft version 13.0.1.0 somewhere locally on your machine
  2. register Newtonsoft.Json.dll in GAC by executing gacutil.exe /i "C:\temp\Json130r1\Bin\net45\Newtonsoft.Json.dll"
  3. Run your application which contains above C# code including the copy constructor.

For reference to cleanup your local machine again:

To unregister the assembly from GAC run gacutil.exe /u Newtonsoft.Json

manuelmainz commented 1 year ago

Similar issues:

readyjoseph commented 6 months ago

We are having similar issue. The azure build pipeline artefact throws "Could not load file or assembly 'Newtonsoft.Json, Version=13.0.1.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)" But locally published artefact works without issue. The newtonsoft version in both packages is the same. So, I am lost what could be the cause of this issue.

EDIT: I downgraded from 13.0.3 to 13.0.1, but this has a bizarre affect. It works in couple of places and fails in another with the same issue.

NOTE: Again - it works fine if I publish it locally and apply it. But the behavior observed when using package from Azure pipeline. Is this an issue with Azure vms?

JustinSchneiderPBI commented 4 weeks ago

Several other examples of the issue popping up. https://developercommunity.visualstudio.com/t/cannot-sign-in-to-visual-studio/105291 https://www.telerik.com/forums/newtonsoft-json-dll-is-installed-in-the-gac https://forum.devolutions.net/topics/24268/newtonsoft-json-in-gac And we have customers reporting this issue to us for Power BI Desktop as well. Please address this years-old issue.

JustinSchneiderPBI commented 4 weeks ago

Here's a PR attempting to make the stated adjustment: https://github.com/JamesNK/Newtonsoft.Json/pull/2976. It needs a test and the change + fix need to be verified.