简体   繁体   中英

How to handle NuGet dependency version resolution for the whole solution

I'm looking for a simple way to manage NuGet packages for the whole solution, to prevent conflicts between transitive NuGet packages when assembling all files into one installer.

When building a project all direct and indirect dependencies are analyzed and the NuGet resolution picks up the best matching version for each NuGet that is at least the same version as the lowest version and might also create binding redirects if necessary. (all good and fine)

The problem we have lately encountered was when we build the whole solution (200+ projects) at once, the resulting NuGet versions between all top level projects might not be identical. And due to the fact, that all resulting DLL and EXE files are installed into the same program files folder, the application can and will crash at runtime due to version mismatches when loading assemblies.

To better understand this issue I've created this sample repo .

The dependency graph looks like this:

  • Library1
    • Microsoft.IdentityModel.Tokens-5.2.1
  • Executable1
    • System.IdentityModel.Tokens.Jwt-5.3.0 ( transitive reference: Microsoft.IdentityModel.Tokens-5.3.0 )
    • Library1
    • results in : Microsoft.IdentityModel.Tokens-5.3.0
  • Executable2
    • Microsoft.IdentityModel.Tokens-5.2.1
    • results in : Microsoft.IdentityModel.Tokens-5.2.1

To demonstrate the problem, all projects compile to the same bin folder. When the whole solution is compiled and Executable2 is started, the application crashes, since the application expects Microsoft.IdentityModel.Tokens in version 5.2.1 but the actual version is 5.3.0.

For this constructed sample it is easy to find the problem and fix it with updating the Microsoft.IdentityModel.Tokens NuGet to the same version. (Manually, since Visual Studio Package Manager does not recognize this conflict in the consolidate tab ).

But at a much greater scale it is far more complex to find those mismatches.

What we have found so far

Centrally managing NuGet package versions

  • Since it is not yet available, it cannot be used to solve the issue here.

Microsoft.Build.CentralPackageVersions

  • Unfortunately there is no IDE support for it, which makes managing NuGet packages very uncomfortable, which I would like to avoid if possible.

So my question is what is the best approach to avoid NuGet version conflicts between projects within the same solution?

We've experienced the same problem with some of our projects. We've been using Paket package manager since a couple of years and this has resolved that issue for us.

In short: you define on your solution level which packages you want to use in a file called ' paket.dependencies '. You can be very specific about versions, or let packet use the latest greatest. Then you can specify per project which NuGet package you want to use within that project in a ' paket.references ' file. As the name implies, you reference to a package in the paket.dependencies file.

This will make sure, all references packages in your project will use the same package version. I hope this suits your needs as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM