Post

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

  1. Place Directory.Packages.props file in the root of your solution
    1
    2
    3
    4
    5
    6
    
     Directory.packages.props
     Solution.sln
     src/
     -- Project1.csproj
     -- Project2.csproj
     -- ProjectN.csproj
    
  2. 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>
    
  3. In related .csproj leave only PackageReference without version specified like
    1
    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>
    
  4. Now you have a central place to manage your NuGet versions (also this is supported via Rider/Visual Studio)

Tips & Tricks

This post is licensed under CC BY 4.0 by the author.