Introduction

For a recent project, I wanted to add a property to the consuming applications from within my NuGet package. This prevents making a pull request for every consuming application with a .csproj change.

๐Ÿ““ Please note in this example the NuGet package has the ID: MyProject.ExampleNuGet, so replace that value for your nuget package. The consuming application is MyProject.ConsumingWebApi.

NuGet file structure

1
2
3
4
5
6
7
8
MyProject.ExampleNuGet  (Repository level)
 โ”ฃ MyProject.ExampleNuGet
 โ”ƒ โ”ฃ Extensions
 โ”ƒ โ”ƒ โ”— MySpecialThing.cs
 โ”ƒ โ”ฃ MyProject.ExampleNuGet.csproj
 โ”ƒ โ”— MyProject.ExampleNuGet.props
 โ”ฃ MyProject.ExampleNuGet.sln
 โ”— nuget.config

MyProject.ExampleNuGet.props

The NuGet package has a .props-file to enforce some properties to the consumers.

1
2
3
4
5
6
7
8
<Project>
  <PropertyGroup>
    <!-- Enable output XML Docs for Swagger. -->
    <GenerateDocumentationFile>true</GenerateDocumentationFile>
    <!-- Disable missing XML comment warnings. -->
    <NoWarn>$(NoWarn);1591</NoWarn>
  </PropertyGroup>
</Project>

MyProject.ExampleNuGet.csproj

Important is to set the build action of the MyProject.ExampleNuGet.props file to package it to the build directory. See the example below:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <None Update="MyProject.ExampleNuGet.props" Pack="true" PackagePath="build">
    </None>
  </ItemGroup>

</Project>

Conclusion

When installing this package on for example MyProject.ConsumingWebApi a file is generated in de build folder MyProject.ConsumingWebApi.csproj.nuget.g.targets. This ensures the setting is on when building MyProject.ConsumingWebApi.

References

I used the following resources to fix my problem.