NuGet: Managing Package Versions in an app - Directory.Packages.props file
appdev csharp nuget wpf
Been working on some multi-project solutions where there is one app with a number of project libs that it depends upon. Problem is, VERSIONING when they depend upon the same NuGet packages. If the version of the same package is different on different projects in the same solution where there are dependencies there can be build issues. One way around this is to code such that a NuGet package package is referenced by only one project in the solution. This is not always possible. The alternative is to have Directory.Packages.props file in the root of the solution which specifies all NuGet packages along with their versions. Each project file then does not specify a version in its NuGet references. For example:
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="CommunityToolkit.MVM" Version="8.4.0" />
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="9.0.9" />
</ItemGroup>
</Project>
Directory.Packages.props
This is placed in the root of the solution. The ManagePackageVersionsCentrally property indicates that package versions will be managed in this file. The PackageVersion elements specify the packages and their versions.

The props file in the root of the solution
This means, for example, that all projects in teh solution that use the CommunityToolkit.Maui package will use version 8.4.0. This avoids version conflicts and makes it easier to manage package versions across multiple projects in a solution. You do not put a version in the csproj file for the package reference, otherwise you will get build errors. For example:.
<Project Sdk="Microsoft.NET.Sdk">
...
...
<ItemGroup>
...
<PackageReference Include="CommunityToolkit.Mvvm" />
...
</ItemGroup>
Relevant sections of a solution’s .csproj project file
An added benefit of this approach is that updating package versions becomes easier. You only need to update the version in the
Directory.Packages.propsfile, and all projects that reference that package will automatically use the updated version during the next build. Also if you use teh tools in, say’ Visual Studio to update a NuGet package, it will update the version in the props file.
There is more information on this approach in the Microsoft Docs at Manage NuGet package versions in a central file.
There is a a further benefit that I found with respect ignoring warnings that I have found. Whilst it is better to resolve warnings, sometimes you may want to ignore them. You can do this in the Directory.Packages.props file as well. For example:
<PropertyGroup>
<NoWarn>XC0022;NU1507;NU1602;NU1604;NU1608
</NoWarn>
</PropertyGroup>
Ignoring Warnings in Directory.Packages.props
These settings are then applied to all projects in the solution. 😊
Conclusion
This approach simplifies package management in multi-project solutions and helps maintain consistency across projects.
| Topic | Subtopic | |
| < Prev: | Jekyll | Equations using Latex |
| This Category Links | ||
| Category: | Application Dev Index: | Application Dev |
| Next: > | AthStitcher | Now a DBMS |
| < Prev: | Photo Timing Video | Video Stitching Detecting Gun Sound |