Picking the right dependency version adding packages from NuGet.org
Since NuGet 2.8, the NuGet Package Manager Console’s Install-Package command comes with support for specifying the dependency version resolution process using the –DependencyVersion switch. If you have never used it before, what it does is it allows you to specify how NuGet should resolve package dependencies. It can resolve dependencies to the lowest possible version (default behavior), the highest possible version, or the highest minor or patch version.
This feature is useful because it gives you control over how dependencies are resolved. For example, when package A has a dependency on package B >= 2.0 and available versions of package B are: 1.0, 2.1.1, 2.1.2, 2.2.1, 2.2.2, 3.0.1, 3.0.2, the version of package B that will be resolved will be:
- 2.1.1, when DependencyVersion is Lowest
- 2.1.2, when DependencyVersion is HighestPatch
- 2.2.2, when DependencyVersion is HighestMinor
- 3.0.2, when DependencyVersion is Highest
Word of warning: changing the default behaviour may break package dependencies. In theory, only the lowest defined version number is sure to work. If a dependency is specified for packages >=1.0.0, using DependencyVersion Highest would potentially bring in version 7.0.0 which may not work for the package depending on it. Use with caution!
If you are interested in always having the latest versions of a dependency within a given mayor version, the HighestMinor setting will actually do that. This can be done from the Visual Studio Package Manager Console:
This can also be set as a default for all NuGet packages installed by editing the NuGet.config file or using NuGet configuration inheritance:
<configuration> <config> <add key="DependencyVersion" value="HighestPatch" /> </config> </configuration>
When adding packages from an upstream feed like NuGet.org or a TeamCity server to a MyGet feed, this setting is available as well:
This should greatly help in getting your preferred packages installed instead of having to install and manually update dependency versions to the required version for your project. For more information about the DependencyVersion switch, do check the NuGet documentation.