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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s