Skip to content

NuGet PackageReference Versions SOLUTION Wide

Sponsor: Do you build complex software systems? See how NServiceBus makes it easier to design, build, and manage software systems that use message queues to achieve loose coupling. Get started for free.

Learn more about Software Architecture & Design.
Join thousands of developers getting weekly updates to increase your understanding of software architecture and design concepts.


NuGet PackageReference Versions SOLUTION Wide

If you have a solution with many projects, it can be a challenge to make sure each NuGet Package has the version across all of your projects. Each PackageReference across all csproj files must specify the same version of the NuGet Package. Here are a couple of ways for managing NuGet PackageRerence Versions across your entire solution.

YouTube

Check out my YouTube channel where I created a video that accompanies this blog post.

PackageReference

First, in order for this to work, you must be using the new SDK style csproj files. This is where your NuGet packages are referenced via the <PackageReference> element.

If you haven’t migrated to the SDK style csproj, check out my migration blog post.

Here’s an example of the eShopOnWeb.Infrastructure.csproj

In this solution, I also have another project (eShopOnWeb.Web.csproj) that is also referencing the same version of Microsoft.EntityFrameworkCore.SqlServer package.

These PackageReferences must always have the same version matching otherwise you’re going to run into issues.

Directory.Build.targets

Instead of making sure each csproj has the same version of a package, you can centralize this by using a file called Directory.Build.targets.

Start off by creating a file called Directory.Build.targets at the same level as your solution file (.sln). This file will contain the PackageReference along with the version. Here’s an example.

This looks very similar to a csproj and how PackageReference’s are used. However there’s one important distinction. Instead of Include attribute, it’s instead Update.

Now back to our csproj, we can now remove the version from the PackageReference

Solution Wide PackageReferences

Another cool use for Directory.Build.targets is to have PackageReferences that are included for every project in your solution. To do this, simply add a PackageReference just as you normally would in a csproj including the version.

In the example below, I’m adding Microsoft.CodeAnalysis to every project.

Note that for this package, we’re using the Include attribute.

Directory.Packages.props

The second way of this is using the new (as of .NET Core SDK 3.1.300) Directory.Packages.props.

Stuart Lang posted about this and how to use it. Check out his blog post Managing Package Versions Centrally.

If you’re using another method, let me know in the comments, on Twitter, or on the YouTube video.

Related Links

Learn more about Software Architecture & Design.
Join thousands of developers getting weekly updates to increase your understanding of software architecture and design concepts.


Leave a Reply

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