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'">
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release45|AnyCPU'">
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:
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.
There are a number of situations when you need to reinstall and update nuget packages for a VS project.
One of this situations is when you wanna upgrade or downgrade the .NET Framework.
In this situation the most important Nuget functionality is Reinstalling packages with dependencies:
reinstalls the same version of the original package, but installs the latest version of dependencies unless specific version constraints are provided. This allows you to update only the dependencies as required to fix an issue. However, if this rolls a dependency back to an earlier version, you can use
to reinstall that one dependency without affecting the dependent package.
For more informations please visit:
In this article we discuss a simple way to create, configure and publish your first c# library through personal NuGet repository.
- Download NuGet.exe from https://docs.nuget.org/create/creating-and-publishing-a-package
- Add environment variable path that refer the installed nuget.exe file
- Build your project in Release mode
- Get the “bin” folder path where you can retrieve the project assembly
- Move under your project bin/release folder and copy the generated *.ddl (pay attention: copy only your project assembly file and not all the dependencies) and paste in a new different folder
- In this folder create these two folders:
- lib – contains only the project dlls
- content – contains all the solution content after the build process (Release mode) without the “bin” and “obj” folders
- Open cmd with admin priviledges:
- move under the temp folder where you create the folder at the step above
- enter in the lib folder (> cd lib)
- run > nuget.exe spec MyAssembly.dll
- this creates a Nuspec file
- move the .nuspec file outside the lib folder
- you must now have this configuration inside the folder:
- edit the NuSpec file as needed (see the section below)
- then run > nuget.exe pack MyAssembly.nuspec
- a new MyAssembly.nupkg was created.
Nuspec file configuration
<releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
<dependency id="Newtonsoft.Json" version="7.0.1" />
<dependency id="NLog" version="4.2.1" />
<dependency id="NLog.Config" version="4.2.1" />
<dependency id="NLog.Schema" version="4.2.1" />
In the .nuspec file you can configure all the dependencies that you dll requires. In this way the NuGet repository can download the dependencies when a client install your NuGet package.
Now you can provide this package and retrieve via Visual Studio as all the other libraries.