Introduction to Nuget Package

It was not a long time when I moved to Visual Studio 2010 for a project and first time listen for Nuget packages and after that day by day these nuget packages became and important part of my development environment. With almost 20k unique packages from the initial release in 2010 the community expanding exponentially and now this plugin became for .net in the same way ‘RubyGems’ stays to ruby by providing a centralized repository for thousands of packages.

Nuget is a free and open source package manager for dot net framework.NuGet is distributed as  a visual studio extension, integrated with sharp developer , and included in the C# code snippet tool LINQPad. NuGet can be used from the command line and automated via scripts.

According to the Nuget

“NuGet is the package manager for the Microsoft  development platform including .NET. The NuGet client tools provide the ability to produce and consume packages. The NuGet Gallery is the central package repository used by all package authors and consumers”

For a developer there is nothing better then finding a library on a centralized place and install it in the project in just blink of the eye. Oh excuse me! means there is more better then finding a package by creating a package and provide it to vast community of developers.

Creating a package: 

Suppose a simple and very common scenario where you have prepared a Dynamic Linked Library(DLL)  having some custom functionality. This DLL also having some HttpModules and Handers that need to register in config file Let’s Count on Non Nuget Package scenario.

Non Nuget Approach :

  1. You will develop the DLL with various versions to support various dot net versions.
  2. You will upload the DLL on some public platform from where user can access it.
  3. You will ask the user that for which version of dot net framework you want to download.
  4. User will download the DLL, will unzip it and will set a reference in his project.
  5. You will provide the guideline to user to manually modify the Web.Config file.

So here you are giving lot of responsibility to the client and that is not going to be very useful. Apart from this there may be various issue like your DLL and client’s project both are using some other DLL with version mismatch. So Client need to upgrade the version to let it work.

Nuget Approach: 

  1. You will create a Nuget package.
  2. Client just need to install the package.

That’s it. Client need to do nothing after that. Everything is prepared. So let’s go through

Steps to create Nuget Package:

  1. Download the Nuget .exe from http://nuget.codeplex.com/releases/view/100803 .
  2. Add a Folder in somewhere on your machine.
  3. Copy the Nuget.exe to the folder.

Nuget-exe

 

4. Open the Command Prompt as Admin and go to the folder.

Admin Command

 

5. Run Command ‘Nuget Spec’ or ‘Nuspec spec Some Name’ on command Prompt.

Admin

 

Now your ‘nuspec’ file has been created in your folder

nuspec

7. This ‘nuspec file” is a xml file. You need to edit it and add some important information like id,version,owner,release note,dependencies,reference etc.

nuspec file

If you need more information about all these tags you can visit http://docs.nuget.org/docs/reference/nuspec-reference

After editing this file save and close the file and move to Command Prompt again.

  1. Before going forward we need to have a look on the folder structure for Nuget package.

To create a package you can layout a directory structure that follows the NuGet conventions.

  • tools – The tools folder of a package is for powershell script and programs accessible from the Package Manager Console. After the folder is copied to the target project, it is added to the `$env:Path (PATH) environment variable.
  • lib – Assemblies (.dll files) in the lib folder are added as assembly references when the package is installed.
  • content – Files in the content folder are copied to the root of your application when the package is installed.
  • build – The build folder of a package is for MSBuild targets files that are automatically inserted into the .csproj file of the application.

Think of the Content folder as the root of your target application. For example, if I want a package to add an image in the /images directory of the target application, make sure to place the image in the Content/images folder of the package.

9. After adding the folders we need we need to add the framework version inside the folders. This will go like

“lib\{framework name}{version}”

i.e.

If you want to add some DLL you will do like this

 

\lib
\net11
\MyAssembly.dll
\net20
\MyAssembly.dll
\net40
\MyAssembly.dll
\sl40
\MyAssembly.dll

We need to follow this folder structure in ever folder in Nuget package (tools,lib,content,build)

10. We are focusing over our DLL so we host need to add a lib folder. As we are just focusing over .net 4.0 and 4.5 framework we would add only these sub-folders and would add our DLL in these folders.

11. We need to register our module too. So we will add xdt files and will add in Content folder in the same way. To know more about config file modifications please go through the MSDN in http://msdn.microsoft.com/en-us/library/dd465326%28v=vs.110%29.aspx

If you want to test your xdt file you will check it online
https://webconfigtransformationtester.apphb.com/

So now our folder structure would look like as :

folder structure

 

folder structure 1

Our work is  almost done. We just need to move to command prompt to create the nuget package

12. Go on Command Prompt and write down following command ‘nuget pack yourpackage.nuspec ‘

Admin-1

Now move to your folder and you will see your package there.

MyFile

 

Congratulations !!
Your package is done.

Now You just need to upload it over Nuget.

Steps to Upload Package over Nuget:

  1. Create an account on Nuget website or sign in with Microsoft account by going on the URL https://www.nuget.org/users/account/LogOn?returnUrl=%2F

 

Nugetgallery

2. Now Click on Upload Package Tab and Click on Upload Package by browsing your file and click on Upload Button.

NugetUpload

3. Now you need to verify some details and then click submit

NugetUpload1

4. Now your package is uploaded

NugetUpload2

Now you just need to go on your project Package Manager console and type the command given and your package would be installed in your project. 

 

Reference :
I found the following link very useful to understand these concepts:
http://docs.nuget.org/ 

If you like this article and want to write me back to discuss over this further, you are welcome to leave comments here.
Don’t forget to like and share..:)

Written By: Ashish Kumar, Software Engineer, Mindfire Solutions

Advertisements

Posted on July 7, 2014, in ASP.Net, C# and tagged , , , , , , , , , , , , . Bookmark the permalink. Leave a comment.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: