Search code examples
c#.netnugetnuget-packagedotnet-cli

.NET — Managing NuGet packages’ common dependencies with different versions


I have two solutions: SolutionA and SolutionB (in separate repositories). Both solutions have dependencies on Package1, but projects under SolutionA leverage version 1.0.0 of this package, but projects under SolutionB leverages 2.0.0.

Now, a need arises to pack and publish a NuGet package of a library in SolutionB, which will be consumed in libraries in SolutionA.

As expected, I see a downgrade error when trying this. So, I decide to downgrade the version of Package1 in SolutionB to version 1.0.0. Code still compiles; there are no bad side-effects during run-time. Then, I pack and publish and try to reconsume in SolutionA edit: At this point, I have tried reinstalling the newest version of the SolutionB NuGet package by both uninstalling it completely then reinstalling and just downgrading it directly within NuGet Package Manager (w/e it’s called..) within VS

At this point, I still see a downgrade error, citing that the SolutionB dependency is leveraging a higher version, but in reality, it isn’t anymore. Why?

Halp!!

edit: adding the error for reference. I chose Newtonsoft.Json to demonstrate. This is easy to reproduce with, really, any NuGet package with multiple versions

Error NU1605 Detected package downgrade: Newtonsoft.Json from 11.0.2 to 9.0.1. Reference the package directly from the project to select a different version. TestProjectA -> TestProjectB 1.0.0 -> Newtonsoft.Json (>= 11.0.2) TestProjectA -> Newtonsoft.Json (>= 9.0.1)

TestProjectA's NuGet dependencies:

enter image description here

TestProjectB's NuGet dependencies:

enter image description here


Solution

  • Make sure that when changing the TestProjectB, you either increase the NuGet Package version or clear your local NuGet caches (dotnet nuget locals all --clear).