In general, when creating a package nuget it is intended to be as inclusive and less restrictive as possible, and can be used in many different projects, regardless of the version of the framework that is used by those who import their package. Although they have been around for a long time, the use of multi-target Packages has become especially popular with the introduction of . net core, since netstandard and . net core did not yet have all the features present in the "full framework".
Imagine a scenario where a package for . net 4.5 also needs to be used in . net core 2.* or 3.*. A practical way to allow the use of packages in both strands of . net is to use the netstandard
, in particular the netstandard 2.0
, for its wide compatibility (due to the significant increase in its api). However, the netstandard 2.0
is only compatible with versions above . net 4.6.1. In this case, you would need to create a multi-target package. Note: To confirm the compatibility of netstandard, you can use this page.
How to create a multi-target package will depend on how your package is created, but by default, just create a directory for each target within lib/
. The complete list of "targets" can be found on this page.
Another way to create this type of package is by specifying distinct dependencies for each target in your file . nupkg.
<dependencies>
<group targetFramework="net45">
(...)
</group>
<group targetFramework="netstandard2.0">
(...)
</group>
</dependencies>
If you are not directly manipulating the package construction, one way to create a multi-target package is to create a "Class Library" project and change the TargetFramework
for TargetFrameworks
no csproj. ex:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net45</TargetFrameworks>
</PropertyGroup>
</Project>
Thus, when executing the command dotnet pack
a properly configured multi-target package will be created.