You have probably encountered situations where, after creating a project, you need to configure TargetFramework, ImplicitUsings, and other properties within the PropertyGroup section of every .csproj file across your projects.

Alternatively, you may have worked on an older project and noticed that the same package is installed in two or more layers. In such cases, you always need to make sure that when updating a package, you also update it in every other layer that references it.

You can solve these problems by creating two files: Directory.Build.props and Directory.Packages.props. Using these files, you can centrally manage your project’s build settings and package versions.

The Directory.Build.props and Directory.Packages.props files should be placed in the root directory of your project, as shown below:


MySolution/
│
├── Directory.Build.props
├── Directory.Packages.props
│
├── src/
│   ├── WebApp/
│   │   └── WebApp.csproj
│   │
│   ├── ClassLibrary/
│   │   └── ClassLibrary.csproj
│   │
│   └── Infrastructure/
│       └── Infrastructure.csproj
│
└── tests/
    └── WebApp.Tests/
        └── WebApp.Tests.csproj

Directory.Build.props

In this file, you should define the settings related to building your projects. For example:

<Project>

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

</Project>

With this file in place, WebApp.csproj, ClassLibrary.csproj, and Infrastructure.csproj will inherit these settings, so there is no need to manually modify each .csproj file to configure them individually.

Directory.Packages.props

In this file, you can define the versions of the packages used throughout your solution. For example:

<Project>

  <PropertyGroup>
    <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
  </PropertyGroup>

  <ItemGroup>
    <PackageVersion Include="Mediator.Abstractions" Version="3.0.2" />
    <PackageVersion Include="Mediator.SourceGenerator" Version="3.0.2" />
    <PackageVersion Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="8.0.28" />
    <PackageVersion Include="Scrutor" Version="7.0.0" />
  </ItemGroup>

</Project>

Next, you should remove the version numbers from the package references in the other project layers that use these packages, allowing the versions to be managed centrally.

For example, in your executable project, the .csproj file should look like this:

<Project Sdk="Microsoft.NET.Sdk">

    <ItemGroup>
        <PackageReference Include="Mediator.Abstractions" />
        <PackageReference Include="Mediator.SourceGenerator">
            <PrivateAssets>all</PrivateAssets>
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
        <PackageReference Include="Scrutor" />
    </ItemGroup>
    
</Project>

This way, you no longer need to specify package versions in your .csproj files. Instead, all package versions are managed centrally through Directory.Packages.props.

Powered by Froala Editor

Comments