Select Page

Sometimes during the development stage, you need to add package references to your project that are only needed either during the development or compilation stage but they are not actually required by the emitted assembly.

So how do we avoid to flood the end project with package dependencies that are actually not needed? Well the answer is super simple but is not obvious, so let’s see the following example

Now we can use the package explorer to open the package produced by the csproj above.

 

 

as you can see the package Newtonsoft.Json is included as a dependency since we include it on the csproj as package reference. So how can we fix that? the answer depend on how now you create your NuGet package, in this case, I’m going to focus my answer on excluding the dependency in a package created by the info in the csproj file (there is a different approach if you use the┬ánuspec file).

To exclude a package reference you have to add the private asset attribute as shown in the image below.

 

<PackageReference Include="Newtonsoft.Json" Version="12.0.2">
  <PrivateAssets>all</PrivateAssets>
</PackageReference>

 

You can learn more about this attribute on the following link

Now if we open the package produced by the csproj above we will see that now there are not dependencies listed

So that is how we get rid of development dependencies, to learn more about how to package a NuGet I recommend the following link

https://docs.microsoft.com/en-us/nuget/reference/nuspec