Creating and Publishing Your Own NuGet Package

NuGet is one of the best Visual Studio extension that simplifies the handling of third-party libraries. It automatically configures Visual Studio projects by adding, removing or updating the references to all the necessary assemblies.

In this post I’ll show you how to create your own NuGet package and publish it to the official NuGet server or to your own NuGet server. So let’s get started.

Installing NuGet Package Manager

In Visual Studio 2010, NuGet is available through the Visual Studio Extension Manager.

Starting with Visual Studio 2012, NuGet client is fully integrated in every edition by default.

In this Example we’ll be using Visual Studio 2015. To check if any NuGet update is available, we’ll navigate to Tools -> Extensions and Updates… and we’ll click the Updates tab.

nuget01

We’ll select NuGet in the list and then we’ll click Update. When the update is completed, we’ll close and re-open Visual Studio.

Installing NuGet Tools

To interact with NuGet directly from the command line, we’ll install the NuGet.exe client tool(version 3.X).

nuget02

We’ll add NuGet.exe to the PATH environment variable.

Next, we’ll install the NuGet Explorer Package to interact graphically with NuGet packages. This is a ClickOnce application, so no installation steps are required.

Creating NuGet Package

Building the Solution

In this example we’ll be using the source code of my last post Using Font Awesome in WPF to demonstrate how to create a NuGet package.

First of all, we’ll create a new class library project by selecting Project from the File -> New Menu.

Under Installed -> Templates ->Visual C#, we’ll select Class Library.

We’ll enter FontAwesomeWPFNuget for the solution Name and we’ll click OK.

Next, We’ll copy the following files from the downloaded source code:

  • We’ll add font-awesome.css and fontawesome-webfont.ttf to the fonts folder.
  • We’ll add CssParser.tt and FA.ttf to the project.

Now we’ll build the solution, and if no errors are encountered, we’re ready for creating a NuGet package.

We’ll cover package creation through the interactive GUI and then we’ll do the same with the command line.

Creating NuGet Package with NuGet Package Explorer

We’ll run NuGet Package Explorer and we’ll select Create a new Package from Common tasks.

nuget03

Next, we’ll select Edit -> Edit Metadata to edit the created metadata file.

nuget07

We’ll enter the necesseray information:
Id: FontAwesomeWpf
Version: 1.0.0.0
Authors: Hicham Benkirane
Description: Using Font Awesome, the iconic font and css tookit in WPF Apps.

nuget08

By navigating to CONTENT -> Add -> Lib folder, we’ll add a library folder that will contain assemblies. Since we are creating a package targeting the .NET Framework Version 4.5, We’ll create a folder for this specific .NET Version.

nuget09

We’ll right-click on the lib folder in the Package contents and we’ll choose Add .NET folder -> v4.5.

nuget10

We’ll add the assembly file generated from the last build to the library folder by selecting net45 folder in the Package contents pane and then we’ll select CONTENT-> Add -> Add existing file…

We’ll press Ctrl+S to save the package.

To check the generated manifest file, we’ll navigate to EDIT -> Edit Metadata Source. we should see and XML similar to the following:

Creating NuGet Package from command-line

First of all, we’ll navigate to the generated assembly file by entering the following command from the command line:

Next we’ll generate the nuspec file directly from the assembly file by typing the following command:

The nuspec file contains all the elements with default values. By opening this file with a text editor, we’ll see the specification file in the XML-format. We’ll just copy the generated XML from NuGet Package Explorer:

Next, we’ll create a library folder and a framework version folder to the assembly. We’ll do this by typing:

We’ll copy the assembly file to his directory:

Finally, we can pack our package by using:

Publishing NuGet Package

The first thing we’ll have to do before publishing our package to the official NuGet server is to create a NuGet account.

To create an account we click the register link, and then we’ll provide an email address, a desired user name, and a password. A confimation email well be then sent to the email address we provided. Once we confirmed that email address, we’re all set to go.

nuget20_0

When our account is activated, we can access its API key. The API key is a secret key that provides us with the token that identifies us in the NuGet gallery.

Publishing NuGet Package with NuGet Package Explorer

In NuGet Package Explorer, we’ll load the previously generated package and we’ll navigate to FILE-> Publish

nuget27

We’ll enter the API key in the Publish key field and click Publish.

To keep the package private and unlisted in NuGet gallery we can check Unlist this package so that it doesn’t appear in the search results.

We can manage our published packages on the NuGet Gallery at https://www.nuget.org/account.

Publishing NuGet Package from command-line

Before publishing the package, we must set the API Key in our NuGet command-line utility:

To publish the package from the command line, we’ll type the following command:

Now, we have successfully published the package using only the command line.

Creating Your Own Nuget Feed

We can host our own NuGet Feed in several ways:

Local folder/Shared folder

The most common way is to simply use a folder in a local filesystem or a shared folder.
We don’t need an API key to push packages. Copying packages into the folder will show them in our feed. By navigating to Tools-> Options we can add the local or shared folder by selecting NuGet Package Manager-> Package Sources

nuget21

NuGet Server

Another method is to create a simple NuGet Server by using the NuGet.server package from the NuGet Gallery.

This package has all the necessary components to get NuGet server up and running in a short time. To host the NuGet Server we’ll need IIS.

We’ll create an empty ASP.NET web application.

In Visual Studio, we’ll navigate to FILE -> New -> Project.

We’ll select ASP.NET Web Application. We’ll name the project as NuGetServer.

nuget22

We’ll select Empty and we’ll click OK.

nuget23

An empty web application is created containing only web.config.

We’ll Install the NuGet.Server package from the official NuGet Gallery.

nuget24

We’ll run the application by pressing F5

A website similar to the following should appear in our web browser:

nuget25

Let’s set the apiKey setting in web.config file to MySecretPassword and publish the package previously created in our newly created NuGet server.

The uploaded package should be by default in our Packages folder.

nuget26

The NuGet Server provides OData sevices for our feed. the published packages is listed at http://localhost:4318/nuget/Packages

The NuGet Gallery

With NuGet Gallery, we’ll have the same experience as we have on www.nuget.org site. The steps to build and run the Gallery can be found at NuGet Gallery Github Site.

Well, that was a very long post so I am going to leave it for now.
If you have any questions, drop me a message in the comments section below.
The full example code can be found at: Github

Leave a Reply

Next ArticleInstalling Oracle Java 8 on Ubuntu 15.10