Nuget – How to!

There is this fascinating packaging and distributing technology called Nuget. You can basically distribute anything, through packages that can be downloaded directly to your development environment. When I say package, I mean library files, scripts, configurations, view files etc., put together in a distributable way.

At a glimpse: I created a new MVC web application and I want to reuse a certain third library available on the internet for datagrid, how do I do it? – Simple, find out a suitable package from nuget.org, download it using Visual Studio Package Manager, Your application will now have all files, scripts, and library(dlls) that’s required to use a datagrid (along with documentation, if available as a part of package).

Intro

As I said earlier, its a packaging and distribution mechanism. Below are certain points that will help you get started.

  • You are creating a web application and would like to distribute web controls you create for re-use
  • Normally, you would package all dll’s put the dependent files in folder and zip it to a location from where people could download your package and copy-paste re-use.
  • On the other hand, lets say you have a nuget driven approach, the entire process is simplified and streamlined.
  • For packaging your web control, you will build the solution using MSBuild scripts, generate output for deployment
  • Create a nuget manifest, which will have information about the component being distributed and its version information.
  • Once you have the manifest and the Build Output, run “Nuget.exe” to get the package created.
  • Host the package at any location, FTP or Web Server.

I am going to explain every step described above, as different topics below.

Why would you package?

Before that, Nuget – this is completely based on what your organisation needs, there are good and bad to it, based on what is required. For example, you don’t have to package an application if it’s not a re-distributable component, or if it cannot accept packages from nuget gallery, for whatever reason that may be. On the other hand, package anything that’s re-usable, Project Template, Custom Controls, Styles, Scripts and so on.

What to Package?

This is the most important question that you requires a lot of analysis. When I say analysis, below are certain stuffs that you need be prepared for, when you decide on what to package.

  1. Target Framework: This is the most crucial item, you need to know which .NET framework your component is targeted for. If your component is not targeted for any particular framework, well and good, your job is less complicated. If your component targets multiple framework, there has to be clear demarcation between different dependencies for each framework.

How To Package?

There are different ways to package using Nuget and they are

  1. Assembly package : A package can be created straight from an assembly based off its manifest.
  2. Project package : You can create a package from .csproj using “Nuget.exe” compiler
  3. Folder based package : Folder based conventional packaging is most flexible and allows multiple framework for packaging.

I’m not going through the details, as they are extensively available at Nuget.org Documentation.

What do I need to Setup Nuget?

Well, you dont need anything special. All you need to do is, go to Nuget.org/Documentation and download “Nuget.exe” – Thats it, you are all set to create your own package.
Now the question is how do we distribute this package. Well thats easier than creating a package. All you need to do is Sign-Up at Nuget.org and upload you package there, after which its available for anyone. That said, you cannot upload enterprise packages onto nuget gallery thats hosted for public. So, there are two different options

  • Host nuget at a FTP location and use the FTP path as feed location in Visual studio package manager
  • Host Nuget Gallery in local environment and provide the Website URL as feed location – Nuget gallery project is open source and anyone can download and host the project at enterprise level, if required with customization.

Host Nuget in local environment using “Nuget Gallery”

Difficulties I faced while hosting this project was with SQL Server configurations, you need to create a proper connection string and update the Web.config to point to an EMPTY DATABASE. When you run the application for the first time, the Nuget Manifest database will be created automatically. The site will also require you to create a FTP location to host nuget packages – basically they will be downloaded or uploaded via application to a FTP location.

  • NUSPEC – .nuspec file is the one which contains manifest details. While you are building the package using .csproj file, this .nuspec file has to be place at the same location as project file.
  • NUPKG – .nupkg is the output nuget package file, this file can be uploaded a nuget gallery hosted locally or onto server at Nuget.org.
Advertisements

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