Target nuget packege for multiple framework

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.

Advertisements

Nuget: How to reinstall and update packages

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:

Update-Package –reinstall

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

Update-Package <dependency_name>

to reinstall that one dependency without affecting the dependent package.

For more informations please visit:

https://docs.microsoft.com/en-us/nuget/consume-packages/reinstalling-and-updating-packages

Create a NuGet package for a C# library

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:
      • content
      • lib
      • MyAssembly.nuspec
    • 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


<?xml version="1.0"?>
<package>
<metadata>
<id>MyAssembly</id>
<version>1.0.0</version>
<authors>Tier-1</authors>
<owners>Tier-1</owners>
<licenseUrl>http://LICENSE_URL_HERE_OR_DELETE_THIS_LINE</licenseUrl>
<projectUrl>http://PROJECT_URL_HERE_OR_DELETE_THIS_LINE</projectUrl>
<iconUrl>http://ICON_URL_HERE_OR_DELETE_THIS_LINE</iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description></description>
<releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
<copyright>Copyright 2015</copyright>
<tags>Tag1 Tag2</tags>
<dependencies>
<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" />
</dependencies>
</metadata>
</package>

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.