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 :
- You will develop the DLL with various versions to support various dot net versions.
- You will upload the DLL on some public platform from where user can access it.
- You will ask the user that for which version of dot net framework you want to download.
- User will download the DLL, will unzip it and will set a reference in his project.
- 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.
- You will create a Nuget package.
- 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:
- Download the Nuget .exe from http://nuget.codeplex.com/releases/view/100803 .
- Add a Folder in somewhere on your machine.
- Copy the Nuget.exe to the folder.
4. Open the Command Prompt as Admin and go to the folder.
5. Run Command ‘Nuget Spec’ or ‘Nuspec spec Some Name’ on command Prompt.
Now your ‘nuspec’ file has been created in your folder
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.
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.
- 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
If you want to add some DLL you will do like this
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
So now our folder structure would look like as :
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 ‘
Now move to your folder and you will see your package there.
Your package is done.
Now You just need to upload it over Nuget.
Steps to Upload Package over Nuget:
- 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
2. Now Click on Upload Package Tab and Click on Upload Package by browsing your file and click on Upload Button.
3. Now you need to verify some details and then click submit
4. Now your package is uploaded
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.
I found the following link very useful to understand these concepts:
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..:)
Posted on July 7, 2014, in ASP.Net, C# and tagged Ashish Kumar, Introduction to Nuget Package, LINQPad NuGet, Mindfire Solutions, Non Nuget Approach, Nuget Approach, Nuget Package, nuspec, RubyGems, Software Engineer, Steps to create Nuget Package, Steps to Upload Package over Nuget, Visual Studio 2010. Bookmark the permalink. Leave a comment.