Central Package Management in .NET 📊
Central Package Management in .NET 📊
Problem
If you have a solution that contains many libraries, then at some point in time there will be a problem with mismatched versions of NuGet libraries.
Something like:
1
2
3
4
5
Solution.sln
src/
-- Project1.csproj
-- Project2.csproj
-- ProjectN.csproj
Solution
Use Central Package Management
- Place
Directory.Packages.props
file in the root of your solution1 2 3 4 5 6
Directory.packages.props Solution.sln src/ -- Project1.csproj -- Project2.csproj -- ProjectN.csproj
- With content
1 2 3 4 5 6 7 8 9
<Project> <PropertyGroup> <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally> </PropertyGroup> <ItemGroup> <PackageVersion Include="Newtonsoft.Json" Version="13.0.1" /> ...rest of your packages </ItemGroup> </Project>
- In related
.csproj
leave only PackageReference without version specified like1 2 3 4 5 6 7 8
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net9.0</TargetFramework> </PropertyGroup> <ItemGroup> <PackageReference Include="Newtonsoft.Json" /> </ItemGroup> </Project>
- Now you have a central place to manage your NuGet versions (also this is supported via Rider/Visual Studio)
Tips & Tricks
- To migrate packages in existing solutions you can use https://github.com/Vannevelj/directory-packages-props-converter or official dotnet upgrade assistant tool https://dotnet.microsoft.com/en-us/platform/upgrade-assistant
This post is licensed under CC BY 4.0 by the author.