Home » Creating NuGet packages for C# libraries

Creating NuGet packages for C# libraries

A few weeks ago I published my first C# NuGet package. The package is a RSS and ATOM FeedReader library that I developed, because existing libraries had issues with encodings, different languages or others. In this blog post, I will quickly describe how to create a NuGet package for a C# library.

The first thing to do is to install nuget.exe. This can be done by downloading the nuget.exe from nuget.org or by using chocolatey. I already blogged about how to use chocolatey (Chocolatey – Package management for Windows) and we will reuse that for the nuget install.

Install nuget.exe

Run Powershell as administrator:

# the following lines install chocolatey
Set-ExecutionPolicy Unrestricted
iwr https://chocolatey.org/install.ps1 -UseBasicParsing | iex

When chocolatey is installed, we can use it to install nuget:

choco install nuget.commandline -y

Creating the NuGet package

I created the NuGet package out of a C# library project. Navigate to the the folder of the C# project and use NuGet to create the .nuspec file which is the specification for the NuGet package. The specification file can be created by executing:

nuget spec

It creates the YourProjectName.nuspec file with the following content:

<?xml version="1.0"?>
<package >
  <metadata>
    <id>$id$</id>
    <version>$version$</version>
    <title>$title$</title>
    <authors>$author$</authors>
    <owners>$author$</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$</description>
    <releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
    <copyright>Copyright 2017</copyright>
    <tags>Tag1 Tag2</tags>
  </metadata>
</package>

As you can see, there are placeholders for the properties. Those will be automatically replaced by the following values from the assembly (AssemblyInfo.cs):

  • $id$: AssemblyName from the project file
  • $version$: AssemblyInformationalVersion if present, otherwise AssemblyVersion
  • $title$: AssemblyCompany
  • $author$: AssemblyCompany
  • $description$: AssemblyDescription
  • $copyright$: AssemblyCopyright

The copyright has by default another value, so I replaced it by $copyright$. The other values must be set manually. My final nuspec file looks as follows:

<?xml version="1.0"?>
<package >
  <metadata>
    <id>$id$</id>
    <version>$version$</version>
    <title>$title$</title>
    <authors>$author$</authors>
    <owners>$author$</owners>
    <licenseUrl>https://github.com/codehollow/FeedReader/blob/master/LICENSE</licenseUrl>
    <projectUrl>https://github.com/codehollow/FeedReader/</projectUrl>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>$description$</description>
    <releaseNotes>fixed issue with pages that do not allow the .net webclient with specific user-agent defined in the header</releaseNotes>
    <copyright>$copyright$</copyright>
    <tags>feed rss atom</tags>
  </metadata>
</package>

Building the NuGet package

When the nuspec file is done, we can build the NuGet package by executing the following command:

nuget pack MyProject.csproj -properties Configuration=Release
# if you have referenced projects:
nuget pack MyProject.csproj -properties Configuration=Release -IncludeReferencedProjects

The result is a nuget package file that you can upload to nuget.org. The filename contains the version. If the version is e.g. 1.0.0.0 – the name will be: MyProject.1.0.0.nupkg, if the version is e.g. 1.3.1.2, then the filename will be: MyProject.1.3.1.2.nupkg.

If you want to publish a prerelease version, you just have to add e.g. “-beta1” to the version – so that the version is e.g. 1.3.1-beta1. If you don’t want to set it in the AssemblyInfo.cs (because of the warning), then you can set it via commandline:

nuget pack MyProject.csproj -properties Configuration=Release -version 1.3.1-beta1

FeedReader libary

As already mentioned, I created a FeedReader library. You can find the source code and a description how to use it on github:
https://github.com/codehollow/FeedReader
and the package on NuGet.org:
https://www.nuget.org/packages/CodeHollow.FeedReader

Additional information

Creating a Package: https://docs.microsoft.com/en-us/nuget/create-packages/creating-a-package

Leave a Reply

Your email address will not be published. Required fields are marked *