How To Create A NuGet Package

NuGet

0 ( 0 votes) | 1877

In this article, we will talk about how to create a NuGet package. Before getting started, let`s first talk about what is NuGet?

What is NuGet?

NuGet is free and open-source package manager for the Microsoft Development Platform. It is distributed as visual studio extension. NuGet client tools provide ability to produce and consume packages. As of writing of this article, version of NuGet is 2.8.5. We will create a class library project containing some extension methods for string and then will publish these extension methods as a nuget package. Let`s get started.

Requirements:

Code:

To get started, open visual studio and create a class library project called “CSharpHelpers” using C# project template and select the target framework as .Net 4.5.  Now create a folder called “StringHelpers” and add “CustomStringHelpers.cs” file under that folder.  Add the following code in CustomStringHelpers.cs file. (Note: Make the CustomStringHelpers class as a static class as we are creating extension methods).

using System;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;

namespace CSharpHelpers.StringHelpers
{
    public static class CustomStringHelpers
    {
        /// 
        /// Remove space from the String
        /// 
        ///String
        /// 
        public static string RemoveSpace(this System.String entity)
        {
            return ReplaceParticularCharacter(entity, " ", String.Empty);
        }

        /// 
        /// Remove last character from the String
        /// 
        ///String
        /// 
        public static string RemoveLastCharacter(this System.String entity)
        {
            return String.IsNullOrWhiteSpace(entity) ? String.Empty : entity.Remove((entity.Length - 1), 1);
        }


        /// 
        /// Remove first character from String
        /// 
        ///String
        /// 
        public static string RemoveFirstCharacter(this System.String entity)
        {
            return String.IsNullOrWhiteSpace(entity) ? String.Empty : entity.Remove(0, 1);
        }


        /// 
        /// Create a string from an array of string using StringBuilder class and specified separator
        /// 
        ///List of string
        ///Separator
        ///
        /// 
        public static string ToString(this System.String[] entities, string separator, bool trim = false)
        {
            StringBuilder str = new StringBuilder();
            foreach (var item in entities)
            {
                str.Append(trim ? (item.Trim() + separator) : (item + separator));
            }
            return str.ToString().RemoveLastCharacter();
        }


        /// 
        /// Replace old character with new character in a string
        /// 
        ///String
        ///Old character
        ///New Character
        /// 
        public static string ReplaceParticularCharacter(this System.String entity, string oldCharacter, string newCharacter)
        {
            return entity.Replace(oldCharacter, newCharacter);
        }

        /// 
        /// Create MD5 Hash of a String
        /// 
        ///String
        /// 
        public static string CreateMd5Hash(this System.String entity)
        {
            var md5Hash = MD5.Create();
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(entity));

            StringBuilder sBuilder = new StringBuilder();
            foreach (byte t in data)
            {
                sBuilder.Append(t.ToString("x2"));
            }
            return sBuilder.ToString();
        }

        /// 
        /// Convert the string to SEO friendly slug
        /// 
        ///String
        /// 
        public static string ToSeoSlug(this System.String entity)
        {
            var str = entity.ReplaceParticularCharacter("&", "and");
            // Remove non word characters
            Regex rgx = new Regex("\\W");

            // Remove multiple dash characters from the string
            Regex rgx1 = new Regex("[-]+");

            var removeMultipleDash = rgx1.Replace(rgx.Replace(str, "-"), "-");

            var removeFirstDash = removeMultipleDash.StartsWith("-") ? removeMultipleDash.RemoveFirstCharacter() : removeMultipleDash;
            var removeLastDash = removeFirstDash.EndsWith("-") ? removeFirstDash.RemoveLastCharacter() : removeFirstDash;
            return removeLastDash.ToLower();
        }
    }
}

Creating A NuGet Package:

To create a package, first we need to create .nuspec file. This is a manifest file that uses XML to describe the package. This manifest file is used to build a package and it’s also stored in package after the package is built. To create this .nuspec file, execute the following command in command prompt.

nuget.exe spec

After executing the preceding command in command prompt, we will get a success message as shown below (F: 1).

Command To Create Manifest File (F: 1)

Default name for the manifest file is Package.nuspec. (I have created this file on my desktop and rename the file to NugetSpec.nuspec). Copy this manifest file and add to our class library project. Let`s open that file in visual studio to see the content. Content of the manifest file are as shown below (F: 2).

Content Of Manifest File (F: 2)

Now let`s go through each and every element in file.

  • id: This is a unique identifier of the package. It`s the name of the package which is used while installing the package using Install-package command in package manager console in visual studio. Package Id follow the same rule as .net namespace. For example Mvc.Helper is a valid package id.
  • version: This is a version of the package like 1.0.0.
  • authors:  This is a comma separated list of authors of the package.

  • owners:  This is a comma separated list of package creators. Most of the time it is same as package authors.

  • licenseUrl: This is a link to the license that the package is under. For example Apache license.

  • projectUrl: This is a link to the project home page. If the project is open-source then it can be the project source code URL like github project URL.

  • iconUrl: This is the link for the image which is used as an icon for the package. Size of the image should be 32×32 pixel png image with transparent background.

  • requireLicenseAcceptance: This is a Boolean value which indicates that whether the nuget client needs to ensure that the package license (specified by licenseUrl) is accepted by the user before installation of the package.

  • description: As the name suggest, this is a long description for the package.

  • releaseNotes: This is a description of the changes that are made in each release of the package.

  • copyright: Specify copyright details for the package.

  • tags: This is a space separated list of keywords which describe the package.  These tags are useful when user tries to search for the package.

  • dependencies: This element specifies the list of other dependent nuget package which should be installed in order to use our package.

  • title: This is a user friendly name of the package. If not specified then package id is used. (By default this element is not included in manifest file.)

  • files:  This element is used to specify the package content. (By default this element is not included in manifest file.)

Now let`s fill the details in package manifest file. Content of updated manifest file are (F: 3).Updated Manifest File Content (F: 3)

Note: Source code for this example is added to my github repository. I am using my github project URL as project URL in manifest file. I have also added the License document to the source code.

NuGet Package Content:

Content of the nuget package can be defined either by defining Convention based folder structure relative to package manifest file or by using files element in manifest file. Convention based approach require the following folder structure:

  • tools: The tools folder of a package is for power shell scripts and programs accessible from the Package Manager Console.
  • lib:  Assemblies (.dll files) in the lib folder are added as assembly references when the package is installed.

  • content: Files under the content folder are directly copied to the root of the application when the package is installed. For example, if we want to add a script file in the /Scripts folder of the target application, then we need to place that file under Content/Scripts/ {jsfilename}.

Now first let`s create the package using the Convention based approach, then we will see how to create the package using files element in manifest file.

Convention Based Approach To Create NuGet Package:

To create package using the Convention based approach, create a new folder called “CSharpHelpersPackage”, add the manifest file that we have created in early steps under that folder. In order to use our extension methods, we need to add reference of our dll in target application. For that purpose let`s create a new folder called lib and add our class library project dll (CSharpHelpers.dll) under that folder (Note: Make sure to copy the dll from bin/Release folder).  Let`s also add a read me file to specify the extension methods included in our package. This file will open up after installation of the package in target application. Content of CSharpHelpersPackage folder are as follows (F: 4)

CSharpHelpersPackage Directory Content (F: 4)

Now it’s time to create the package. To create the nuget package, execute the following command in command prompt.

nuget.exe pack NugetSpec.nuspec

After execution of preceding command in command prompt, we will get a message as shown below (F: 5).

NuGet Package Creation Using Convention Based Approach (F: 5)

According to the message, nuget package is created successfully but there is one warning “Assembly not inside a framework folder“. Solution to resolve this warning is also provided in the same warning message that we need to move the assembly into a framework specific folder.

When we install a nuget package, it determines the framework of the target application and then tries to find the appropriate assembly under the lib folder. While creating our class library project, we selected the target framework as .Net 4.5, so create a new folder called net45 under lib folder and move the CSharpHelpers.dll inside net45 folder.  net45 is the abbreviation for the .Net framework 4.5. For more information regarding the common framework and their abbreviation click here. Now let`s try to create package one more time.  Now this time after execution of the command, we will get a success message as shown below (F: 6)

Created The NuGet Package Using Convention Based Approach (F: 6)

Great! We are able to create nuget package using convention based approach. Now let`s try to create package using files element in manifest file.

Create NuGet Package Using files Element In Manifest File:

To create the package using files element in manifest file, add the following code in manifest file after metadata element.

  
     
     
  

files element in manifest file is just the other way of specifying the content of nuget package. Execute the same command (nuget.exe pack NugetSpec.nuspec) to create the nuget package. We will get the same success message as shown in image (F: 6).

Testing:

This is a very important step. We need to make sure that after installation of the package in target application, our package content are getting placed at the proper place. To test the nuget package, we can create a local feed of nuget packages in visual studio.  To create local feed, create a new folder called MyLocalFeed on any drive (You can name the folder as you like. I have created the folder on my E: drive) and add the nuget package that we have created earlier to this folder. Now to add this local feed in visual studio , open up visual studio, go to Tools => Options. Then search for the Package Sources option. It will be under NuGet Package Manager Option as shown below (F: 7)

Creating Local Feed Of NuGet Package In Visual Studio (F: 7)

To add local feed, click on plus icon. It will add a new package source named as “Package Source” and source value as “http://packagesource” as shown below (F: 8)

Create New Local NuGet Package Feed (F: 8)

Rename the  package source as “Local Feed (You can name the source as you like)” and replace the default source value with path to our local feed folder as shown below (F:9)

Local NuGet Package Feed Configuration (F: 9)

After filling the details, click on Update button. After clicking on Update button, our local package feed will get listed in Available package sources as shown below (F: 10)

Local Feed Is Added As Package Source (F: 10)

Now to test our package, create a new console application. Right click on References and select the Manage NuGet Packages option from the menu. It will open up Manage NuGet Package window. Select the online option from left menu. Under Online option, our Local Feed (Here Local Feed is the name of the package source that we have specified while configuring the local nuget package feed.) will be displayed as a package source as shown below (F: 11)

Install The NuGet Package (F: 11)

As shown in preceding image (F: 10), we can see our nuget package CSharp Helpers is listed under Local Feed. Now click on Install button to install the nuget package. After installation of the package, CSharpHelpers.dll will get added as an assembly reference in our console application as shown below (F: 12)

CSharpHelpers.dll Is Added As An Assembly Reference (F: 12)

Now the next step is to test the extension methods included in our package. Add the following code in Program.cs file.

using System;
using CSharpHelpers.StringHelpers;

namespace ConsoleApplication1
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var str = "How to create a nuget package";
            Console.WriteLine(str.RemoveSpace());
            Console.ReadKey();
        }
    }
}

The preceding code uses the RemoveSpace extension method to remove the space from str variable. Now let`s run the application to see the output. Output is as shown below (F: 13)

Output Of Console Application (F: 13)

As shown in preceding image, spaces are removed from the string variable. Great! We are able to create and test our nuget package successfully.

Conclusion:

In this article, we learn how to create a nuget package and test it by creating a local feed of nuget packages. I hope you enjoyed reading the article.

Happy Coding!!!!

 

Manoj Kulkarni - Dotnetcontext

Manoj Kulkarni

I am programmer, passionate blogger and foodie. I live in Nashik. I am a .Net developer. I like to learn new technologies to keep my self-updated and share that knowledge with my blog readers, friends and colleague.

3 Comments


gaurav - Dotnetcontext

gauravMonday, November 16, 2015 2:57 AM

Nice Post It was really Helpful. !!

Nitin Tyagi - Dotnetcontext

Nitin TyagiMonday, April 11, 2016 3:00 AM

Liked this post. Very good indeed.

Manoj Kulkarni - Dotnetcontext

Manoj KulkarniMonday, April 11, 2016 3:01 AM

Thank you for the feedback Nitin

Add a new comment