A New Place To Manage Packages in .NET

NuGet is an open-source package management system for .NET developed by Microsoft. It allows developers to create, share, and consume reusable code in packages. These packages can include compiled code (DLLs), source code, configuration files, scripts, and other content.

NuGet supports versioning, allowing developers to specify which versions of a package they need and to easily manage updates and rollbacks.

Managing dependencies for a single project is easy. It becomes complicated when there are many projects in the solution.

Let’s see how we can manage NuGet packages from one central place. 

History

For old project types, you could use packages.config file having an XML format. It’s deprecated.

Then, there was the packages.json file in JSON format. It’s also deprecated 🙂

For new-type projects, you should use PackageReference in project files. 

As said, managing versions for a single project is easy. You have one place to change. But it takes work when you have many projects, each with many dependencies. 

Central Package Management

Since NuGet 6.2, you can manage your dependencies in all your projects in one place. You have to add Directory.Packages.props file to your solution. 

It’s an XML file, as you can see. You must add the MSBuild property ManagePackageVersionsCentrally and set the true value. 

Then, you list all your dependencies with <PackageVersion /> elements defining their versions. 

When you build the solution, you’ll get the error:

NU1008    Projects that use central package version management should not define the version on the PackageReference items but on the PackageVersion items: Serilog;Newtonsoft.Json;MediatR.

That’s because from now the Directory.Packages.props file is the only source of truth. So, let’s get rid of the versions in the project. 

That’s it. Now, all versions of dependencies can be managed from one place. 

Many Directory.Packages.props Files

You can have multiple Directory.Packages.props files in your repository when you have several solutions.

The closest to the project Directory.Packages.props file will be used. 

Project1 will use the Directory.Packages.props file in the Repository\Solution1\ directory.

Project2 will use the Directory.Packages.props file in the Repository\ directory.

Overriding Versions

The VersionOverride property allows you to override a specific package version in the project. This can be helpful if you want to test different package versions. 

You can disable overriding versions by setting the MSBuild property CentralPackageVersionOverrideEnabled to false. 

Transitive Pinning

When you add a NuGet package to your project, that package may have its own dependencies, which in turn may have its own dependencies, and so on. These are known as transitive dependencies. By default, NuGet will resolve these transitive dependencies based on version constraints specified by the packages and the rules for version resolution.

Transitive pinning in NuGet explicitly specifies the versions of transitive dependencies.

To enable this feature, you can do it with the MSBuild property CentralPackageTransitivePinningEnabled set to true. 

Summary

Central Package Management in NuGet is a feature that allows you to manage package versions for all projects in a solution from a single location. This centralized approach simplifies dependency management and ensures consistency across multiple projects.

Use the Directory.Packages.props file to define package versions. This file is placed at the root of your solution and applies to all projects within the solution.

It ensures all projects use the same package version, reducing the risk of version conflicts and simplifying updates.

Managing package versions from one file makes updating and maintaining dependencies easier across large solutions.

By adopting central package management, you can streamline workflow, enhance project maintainability, and ensure a more robust and consistent development environment.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top