Coding Guru

Code never does the mistake, we make the mistakes in the code. Think, Imagine, Phrase and then Code.

Nuget Create -Deploy-Use

About NuGet

According to the NuGet’s official page:

NuGet is a Visual Studio extension that makes it easy to add, remove, and update libraries and tools in Visual Studio projects that use the .NET Framework.

In detail Nuget is a free, open-source package management system offered by the Microsoft ASP.Net team to the developer community to ease the process of integrating any 3rd party open source component into their projects. The NuGet client tools offer the ability to produce and consume the packages. By using the client we can always keep the track of any update made into the consumed package and update our project in really simplified steps.

NuGet is a package manager .Net for which an extension is available for VS2010 and by default it’s integrated into VS2012+. The NuGet Gallery is the central package repository used by all package authors and consumers.

By the developers - For the developers

Use existing when you always know it works rather than wasting your resources creating a new one without guarantee of its result. Yes the packages developed by some developers are available on Nuget Gallery which we can simply use without any worries of compilation errors and effort wasting on testing from the scratch.

Microsoft owns around 100,000 employees worldwide but the world has millions of developers working all the time. It doesn’t make sense for Microsoft to attempt to fill every niche, nor does it scale. Developers, therefore, frequently take it upon themselves to “scratch their own itch,” For every new project a developer creates new functional libraries which can be re used by other developers. Those developers publish their package on web for others to search and use. Trust me web is not so compact to search the libraries we need and moreover to select the one which fulfills all our needs. But Nuget Gallery is compact and easy to use tool for the stated purpose.

You can save a lot of time by checking out NuGet.org before google, despite getting fewer results, because what you do find can be tried out so quickly. Using packaged libraries is consistently easier, library maintainers can help out their users by publishing packages for their libraries for the updates which can be easily managed while integrated into the project.

Why do we need it?

Nuget is pain relieving

NuGet is a Visual Studio extension that makes it easy to add, update and remove libraries or other files (deployed as packages) in a Visual Studio project. A NuGet package is a set of files/assemblies packaged up into a single file with the .nupkg extension using the Open Packaging Conventions (OPC) format.

OPC is just a fancy acronym for a zip file with some metadata. The NuGet package has an extension as .nupkg.

Let’s have an example of simple library Log4Net. It has many steps to do before we could use the library for logging purpose. Normally the steps are as follows:

·        Find on Web: Search for the library on internet and download the correct package.

·        Unzip the Package: Unzip the library files from the .zip file

·        Add reference: Add reference for the dll into the the project.

·        Update Assembly Info: Update assembly info file with config file path containing section for Log4net configuration with correct version and assemble information.

·        Add configuration section: Add configuration section for Log4net in configuration file or new files which is provided in assembly information.

·        Optional changes: There are few more optional changes you have to do in config file to customize the library.

Ufffffff, What a brainstorming pain!  It becomes severe when to repeat for few more libraries and worst when to do it all the times you update the libraries. It becomes never ending dark tunnel when you are forced to jump into a problem setting up the library due to lack of knowledge, documentations or the environment. 

Don’t we need an easy way to automate this, Nuget package does it all for us. All we have to do is install the package and let the package serve us. I will configure everything we need, all the changes will be automated and we need to stress out and waste our resources for the setup.

What else it does?

NuGet provide us the feature to deal with software dependencies. Many make the assumption that it's only meant for open source or third party components, but you could equally as well use it for your own internal packages. You have a feature to keep your package private (Hidden from the list in Nuget gallery) or setting up a local network repository which will be available for a network group. Few benefits of using NuGet are as follows:

·        NuGet encourages reuse of components because you implicitly rely on actual "releases" (even if pre-release), instead of branching sources

·        When we use NuGet to install a package, it copies the library files to your solution and automatically updates your project (add references, change config files, etc.). If you remove a package, NuGet reverses whatever changes it made so that no clutter is left.

·        We can get rid of binaries bloating your VCS repositories (package restore feature)

·        It forces package creators to think about the way the package will be consumed and leaves them dealing with configuration of the component during package installation (who knows best how to configure the package than the package creators?).

·        Automating package creation and publication on a package repository effectively is a form of continuous delivery (for software components). Octopus Deploy even takes it a step further and enables packaging entire Web sites ready for deployment.

·        NuGet encourages and sometimes enforces you to follow some ALM best practices. E.g. a package has a version, so you have to think about your versioning strategy (e.g. SemVer.org)

·        NuGet integrates with SymbolSource.org (which also has a Community edition to set up your own): this allows one to easily debug released packages without having to ship this info all the time

·        Having one or more package repositories makes it easy for the organization to maintain a dependency matrix, or even build an inventory of OSS licenses that are in use by several projects

·        NuGet notifies you about available package updates

·        Updating a package updates all the required information which is a pain relieving functionality, we don’t have to change everything all the times we update the package(Not in the case of libraries picked from the web)

·        Creating packages makes people think about component architecture (all dependencies should be packaged as well)

·        Dependencies of a package are automatically resolved (so you can't forget any)

·        NuGet is smart enough to add assembly binding redirects when required

How to install?

NuGet requires Visual Studio 2010 Pro/Premium/Ultimate (or newer), Visual Web Developer Express 2010, or any Express SKU of Visual Studio 2012 (or newer). The NuGet Package Manager Console requires that PowerShell 2.0 be installed. PowerShell 2.0 is already installed if you have the following operating systems:

  • Windows 7 (or newer)
  • Windows Server 2008 R2 (or newer)

If you have the following operating systems, you must manually install PowerShell 2.0.

  • Windows XP SP3 /Windows Vista SP1
  • Windows Server 2003 SP2/ Windows Server 2008

Visual Studio 2012, 2013

Visual Studio 2012, NuGet is included in every edition (except Team Foundation Server) by default. Updates to NuGet can be found through the Extension Manager.  To check if your copy of Visual Studio already has the NuGet extension, look for Library Package Manager in the Tools menu of Visual Studio. If your copy of Visual Studio does not already have the Library Package Manager (NuGet) extension, you can install it using the Extension Manager.

Visual Studio 2010 Pro/Premium/Ultimate (or newer)

For Visual Studio 2010, NuGet is available through the Visual Studio Extension Manager. The extension can be installed into the Professional, Premium, and Ultimate editions. Additionally, the extension can be installed into Visual Web Developer 2010 Express.

Steps:

  • Open Visual Studio 2010.
  • Go to Tools> Extension Manager.
  • Extension Manager Dialog will load all installed extensions installed in Visual Studio 2010. See the below figure. If the NuGet is already installed, it will be listed here.

  • Click “Online Gallery” tab in the above console. This will load all the available extensions for the visual studio. Once loaded, you can find the “Nuget Package Manager” add-in. Click Download button to download and install. Alternatively, you can type Nuget in the search textbox to load only Nuget from Online Gallery.

  • After downloading, the setup will begin automatically. Refer below. Click Install

 

Add packages

In this section, we will see how to use NuGet to find and integrate some 3rd party library using the GUI and PowerShell commands. As we have already installed NuGet extension on 2010. It works the same way in both VS2010 and VS2012. For the update purpose we will use VS2012 in following sections.  We have to options to add packages into our project or solutions:

·        Graphical User Interface

·        Visual Studio Console or PowerShell Commands

 

Adding Packages using GUI

Visual studio provides an efficient feature to add, update or removing the libraries from the project. On successful installation of NuGet, you can see the NuGet option in Tools> Nuget Package Manager. See the image below. A package can be installed by following the steps defined below just in few seconds without any pain. Select “Add Library Package Reference”. This will bring the NuGet GUI as below. The Installed Packages tab will show if there are any library (or Package) already added through NuGet in your project.



Steps:

·        Find the package: Go to the Tools > NuGet Package Manager > Manage NuGet Packages for Solution. A dialog will appear where a list of favorite packages pop up by default. You may choose a package from the list or you have an option to search the package online.


·        Install the package: On right side of every package a button appears as install. Click it to install the package, once installed a green mark will appear which indicates that the package has been installed properly. After installation the list of all projects in the solution will be populated, we may select the projects for which we have to refer the package libraries.


·        Check Reference: Open the project references ad locate the dll reference. For now we have added Elmah.


·        Check Config files: So as in above example the Elmah dll has been added. Now look for the Web Config or App Config for the same project. You will find that the settings for the Elmah has been already populated as follows:


Package Folder: A new folder wil name packages has been created in the solution directoryThe packages folder contains a subfolder for each installed package. This subfolder contains the files installed by the package. It also contains the package file itself (the .nupkg file, which is a .zip file that contains all of the files included in the package).


So now the package and its functions are available to use into our project.

 

Adding Packages using Package Manager Console

In previous section we saw how to add NuGet package into our project through the NuGet package manager. In this section we will learn to add the package through Package Manager Console.

 

Go to the Tools>Library Package Manager > Package Manager Console. The package manager console will be displayed.


The two drop-down lists set default values that let you omit parameters from the commands you enter in the window:

  • In the Package source list, select the default source (NuGet package feed) that you want your commands to use. Typically you will leave this as its default value of NuGet official package source
  • In the Default project list, select the default project that you want your commands to work with. (The default value will be the first project in the solution, not necessarily the one you have selected in Solution Explorer when you open the window.)

These defaults can be altered to change the nuget package source and the project for which package has to be installed.

Steps:

Find the Package: In the Package Manager Console window, enter Get-Package -ListAvailable at the prompt to see a list of all packages that are available in the selected package source.


For the default package source, that command is going to list thousands of packages. It makes better sense to specify a filter.

For example, to find the logging package ELMAH, enter Get-Package -ListAvailable -Filter elmah (the name of the package) or Get-Package -Filter Logging -ListAvailable (a keyword in the package description).