Tags

,

Along with the release of ASP.NET MVC 3 Microsoft also release a new Visual Studio Extension called “NuGet“.  To quote Scott Guthrie from his January 13th announcement, NuGet is “a free, open source, package manager that makes it easy for you to find, install, and use open source libraries in your projects”.

Since the announcement in January I have been using and increasingly loving NuGet.  Basically, NuGet makes adding 3rd party assemblies to your projects very simple.  NuGet will install the latest version from a central repository and during installation will check for, locate, download and install any dependant packages.  It will also, if the package requires it, update your configuration files.  No more searching the web for the latest version, downloading it manually and figuring out how to add it to your project.

Before we can start adding packages to our solutions we need to install NuGet.  You can either download the NuGet Visual Studio extension from their official web site or install NuGet using the Visual Studio Extension Manager.

Once NuGet has been installed there are two (2) usage interfaces to choose from, a GUI window or a Power Shell console.  When I first started using NuGet I started with the GUI window but found myself gravitating to the Power Shell console as I became comfortable with NuGet.

Let’s walk through adding a package to a new project.  I am going to use Ninject, my favourite dependency injection library, for this example.

To add the Ninject library to your project using the GUI window, right click on the “References” node of the target project and choose “Add Library Package Reference”.  Select the “Online” option in the left navigation pane to populate a list of available packages.

Now, in the “Search Online” text box in the upper right corner, enter “ninject”.  The search will execute and filter the results as you type.  Once complete, the result set should include a package named “Ninject”.  Select the package (in the event it is not already selected) and an “Install” button will be displayed to the right of the description text.  Upon clicking the “Install” button an installation status window will appear.  At this point, you might be prompted to accept a license agreement, accepting the agreement will continue the process.  When the installation has completed you should see a check mark where the install button used to be.  Now, close the “Add Library Package Reference” window and look at the project “References”.  You should notice that a new reference for “Ninject” has been added to the project.

Alternatively, you can add the Ninject library to your project using the “Package Manager Console”.  To accomplish this, from the “View” > “Other Windows” menu, select “Package Manager Console” and a Power Shell console window should be added to the Visual Studio IDE.

You can search the package repository using the Power Shell console using the “Get-Package” command with a few parameters as shown.

PM> Get-Package -ListAvailable -Filter "Ninject"

Execute the command and you will be presented with the search results as shown.

As you can see, using this method you are shown all available versions of the package, unlike the GUI that only shows you the latest version.  Depending on your needs, this may be important.

To install the latest version of the Ninject package, execute the following command.

PM> Install-Package Ninject

Once complete you should see the following messages.

You are downloading Ninject from Ninject Project Contributors, the license agreement to which is available at https://github.com/ninject/ninject/raw/master/LICENSE.txt. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'Ninject 2.2.1.0'.
Successfully added 'Ninject 2.2.1.0' to ClassLibrary.

At this point, just as with the previous example, you should now see the “Ninject” library in the “References” node of your project.

The Power Shell console also makes it quite easy to remove previously installed packages from your project.  To remove the “Ninject” package we just installed, use the “Uninstall-Package” command.

PM> Uninstall-Package Ninject

The console will show the actions preformed and the project reference has been removed.

Successfully removed 'Ninject 2.2.1.0' from ClassLibrary.
Successfully uninstalled 'Ninject 2.2.1.0'.

In this example, the package we have been installing does not have any package dependencies.  The previous command will only uninstall the target package and leave the dependent packages installed.  If you wanted to remove the target package and all the dependent packages you can use the “-RemoveDependencies” parameter with the “Uninstall-Package” command.

PM> Uninstall-Package Ninject -RemoveDependencies

One more extreamly useful command to learn is the “Get-Help” command.  The console will display the usage of any NuGet command if you precede the target command with the “Get-Help” command.

PM> Get-Help Uninstall-Package

NAME
    Uninstall-Package

SYNOPSIS
    Uninstalls a package.    

SYNTAX
    Uninstall-Package [-Id] <string> [-RemoveDependencies] [-Force] [-Version <string>] [<CommonParameters>]

DESCRIPTION
    Uninstalls a package. If other packages depend on this package, the command will fail unless the –Force option is specified.

RELATED LINKS
    Online version: http://nuget.codeplex.com
    Uninstall-Package 

REMARKS
    To see the examples, type: "get-help Uninstall-Package -examples".
    For more information, type: "get-help Uninstall-Package -detailed".
    For technical information, type: "get-help Uninstall-Package -full".

There is a myriad of other Power Shell console commands as well.  I encourage you to take the time to read up on the available commands.  As you can see, the console is quite powerful and provides quite a bit more functionality than the GUI window.  Once you get used to NuGet you will probably find it more advantageous to work in the console.  Also, since the console is a Power Shell console, you can use any Power Shell command you wish and even script common actions.  The possibilities are endless.

If you are interested in further reading about NuGet, a good resource is the NuGet web site that has a series of useful usage tutorials.  As well, the hacked site posted an article showing the updates to NuGet in the recent 1.1 release.

As you can see, using NuGet simplifies adding packages to your projects and lets you get to what is really important, building software, faster and with less hassle.  I highly recommend trying out and using NuGet on your next project.

About these ads