How to change the version of Newtonsoft.Json used by netstandard 2.0 library

Viewed 665

I created the netstadard 2.0 class library that references Newtonsoft.Json 12.0.3 nuget. When I build the library and check the referenced assemblies via Assembly.GetReferencedAssemblies() I see that my library references Newtonsoft.Json version 12.0.0 instead of 12.0.3.

Publishing the same library via dotnet publish command copies 12.0.3 version of Newtonsoft.Json as well.

However, if I try to load that netstandard library from .net core 3.1 app dynamically using Assembly.Load() and then use reflection to access type information I get the exception that Newtonsoft.Json version 12.0.0 could not be loaded.

How can I force my class library to reference the 12.0.3 version instead of 12.0.0 which does not even exist on nuget?

1 Answers

The Assembly version in Newtonsoft.Json returns 12.0.0.0 by design. Package Versions and Assembly Versions aren't directly tied together and for various reasons can be different. (I for one sometimes forget to update the assembly version when I increment my package version.)

If you look at the buildScripts.ps1 in the repo you can see where AssemblyVersion is specifically set to the major version plus 0.0 when the AssemblyVersion is empty - which it is, since it isn't set anywhere else in the script.

If multiple packages are referencing different versions of Newtonsoft.Json, then just reference the 12.0.3 package directly in your project. At that point calling Assembly.Load("Newtonsoft.Json") should load the version you want.

Related