Centralized Package Management in .NET Projects

Table Of Contents
Introduction
Managing dependencies in a .NET project can become cumbersome as the number of dependencies grows. Centralized package management helps streamline this process, making it easier to manage and update package versions across multiple projects. In this blog post, we will explore how to manage package versions centrally in a .NET project, ensuring a consistent and maintainable setup.
Setting Up Centralized Package Management
First, let’s take a look at a simplified project file (Directory.Packages.props) that demonstrates how to manage package versions centrally:
<Project><PropertyGroup><ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally><TreatWarningsAsErrors>true</TreatWarningsAsErrors><TargetFramework>net8.0</TargetFramework><AspNetVersion>8.0.6</AspNetVersion><SystemExtensionVersion>8.0.0</SystemExtensionVersion></PropertyGroup><ItemGroup><PackageVersion Include="altcover" Version="8.8.74" /><PackageVersion Include="AutoFixture" Version="4.18.1" /><PackageVersion Include="Bogus" Version="35.5.1" /><PackageVersion Include="Microsoft.AspNetCore.Authentication.Google" Version="$(AspNetVersion)" /><PackageVersion Include="AutoMapper" Version="13.0.1" /><PackageVersion Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" /><PackageVersion Include="coverlet.collector" Version="6.0.2" /><PackageVersion Include="coverlet.msbuild" Version="6.0.2"><IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets><PrivateAssets>all</PrivateAssets></PackageVersion><PackageVersion Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="$(AspNetVersion)" /><PackageVersion Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="$(AspNetVersion)" /><PackageVersion Include="Microsoft.AspNetCore.Identity.UI" Version="$(AspNetVersion)" /><PackageVersion Include="Microsoft.EntityFrameworkCore.InMemory" Version="$(AspNetVersion)" /><PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite" Version="$(AspNetVersion)" /><PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="$(AspNetVersion)" /><PackageVersion Include="Microsoft.EntityFrameworkCore.Tools" PrivateAssets="all" Version="$(AspNetVersion)" /><PackageVersion Include="Newtonsoft.Json" Version="13.0.3" /><PackageVersion Include="NSubstitute" Version="5.1.0" /><PackageVersion Include="Serilog.AspNetCore" Version="6.1.0" /><PackageVersion Include="Shouldly" Version="4.2.1" /><PackageVersion Include="Swashbuckle.AspNetCore" Version="6.6.2" /><PackageVersion Include="xunit" Version="2.8.1" /><PackageVersion Include="xunit.runner.visualstudio" Version="2.8.1" /></ItemGroup></Project>
Key Components
Centralized Package Management Activation:
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>: This enables centralized package management, allowing you to define all package versions in a single place.
Target Framework:
<TargetFramework>net8.0</TargetFramework>: Specifies the target framework for your project.
Version Variables:
<AspNetVersion>8.0.6</AspNetVersion>and<SystemExtensionVersion>8.0.0</SystemExtensionVersion>: These variables store the versions of specific package groups. You can reference these variables in theItemGroupto keep your package versions consistent.
Using Version Variables
In the ItemGroup, you can use the version variables to define package versions. This approach ensures that you only need to change the version in one place when updating packages.
For example:
<PackageVersion Include="Microsoft.AspNetCore.Authentication.Google" Version="$(AspNetVersion)" /><PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="$(SystemExtensionVersion)" />
Benefits of Centralized Package Management
Consistency:
- Ensures that all projects use the same version of a package, reducing compatibility issues.
Maintainability:
- Simplifies the process of updating package versions. You only need to update the version variable, and all references will be updated automatically.
Reduced Errors:
- Centralized management reduces the likelihood of version conflicts, making your project more stable.
Conclusion
Centralized package management in .NET projects is a powerful feature that simplifies dependency management. By using version variables and defining package versions in a single place, you can ensure consistency, maintainability, and reduced errors across your projects. Implementing this approach in your .NET projects will save time and effort, allowing you to focus on writing great code.
Feedback and Questions
Your insights drive us! For any questions, feedback, or thoughts, feel free to connect:
- Email: shady@shadynagy.com
- Twitter: @ShadyNagy_
- LinkedIn: Shady Nagy
- GitHub: ShadyNagy
If you found this guide beneficial, don’t hesitate to share it with your network. Until the next guide, happy coding!







