When you’re building a .Net liibrary you have to take in care which framework the end user will use. In order to avoid user’s framework upgrade/downgrade we must target multiple frameworks in the nuget package.
Library project configuration
First, in your .csproj file, create separate build configurations. I created “Debug” | “Release” and “Debug45” | “Release45” for the target frameworks 4.6.1 and 4.5 respectively. I manually set the OutputPath and added theTargetFrameworkVersion element to each configuration:
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug45|AnyCPU'"> <OutputPath>bin\Debug45\</OutputPath> <TargetFrameworkVersion>v4.5</TargetFrameworkVersion> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release45|AnyCPU'"> <OutputPath>bin\Release45\</OutputPath> <TargetFrameworkVersion>v4.5</TargetFrameworkVersion> </PropertyGroup>
In this way you can build the library with different target framework.
Targeting Multiple Frameworks in NuGet
In the folder created for the nuget package divide the lib folder by adding for each targeted framework the relative folder where you will add the relative library .dll file.
The lib folder looks like this:
- lib
- net45
- net461
Now you can run nuget.exe pack MyAssembly.nuspec
A new package is created.
When you refer this package through Nuget package manager from a VS solution, your reference automatically link the dll related to your project framework.