Wednesday, 1 February 2012

Small Introduction to NuGet

Not too long ago, Microsoft released, NuGet, an automated package manager for Visual Studio.  NuGet makes it easy to download and install assemblies, and their references, into a Visual Studio project.  These assemblies, which I loosely refer to as packages, are often open source, and include projects such as Nhibernate. In this post, I'll explain how to get started in using NuGet with your projects to include: installng NuGet, installing/uninstalling Nhibernate via console command, and installing/uninstalling Nhibernate via graphical reference menu.


Installing NuGet
The first step you'll need to take is to install NuGet.  Visit the NuGet site, at http://nuget.org/, click on the Install NuGet button, and download the NuGet.Tools.vsix installation file, shown below.


Each browser is different (i.e. FireFox, Chrome, IE, etc), so you might see options to run right away, save to a location, or access to the file through the browser's download manager.  Regardless of how you receive the NuGet installer, execute the downloaded NuGet.Tools.vsix to install Nuget into visual Studio.


The NuGet Footprint
When you open visual Studio, observe that there is a new menu option on the Tools menu, titled Library Package Manager. This is where you use NuGet.  There are two menu options, from the Library Package Manager Menu that you can use: Package Manager Console and Package Manager Settings


I won't discuss Package Manager Settings in this post, except to give you a general idea that, as one of a set of capabilities, it manages the path to the NuGet server, which is already set for you. Another menu, added by the NuGet installer, is Manage Nuget packages, found by opening the context menu for either a Solution Explorer project or a project's References folder or via the Project menu.  I'll discuss how to use this later in the post. The following discussion is concerned with the other menu option, Package Manager Console, which allows you to manage NuGet packages.

Getting a NuGet Package
Selecting Tools -> Library Package Manager -> Package Manager Console opens the Package Manager Console.  As you can see, below, the Package Manager Console is text-based and you'll need to type in commands to work with packages.


In this post, I'll explain how to use the Package Manager Console to install Nhibernate, but there are many more commands, explained in the NuGet PackageManager Console Commands documentation.  To install Nhibernate, open your current project where you want Nhibernate installed, and type the following at the PM> Install-Package nhibernate If all works well, you'll receive a confirmation message, similar to the following, after a brief pause: Successfully installed ‘nhibernate 3.2.0.4000’. Successfully added ‘nhibernate 3.2.0.4000’  to MyApplication Also, observe that a reference to the Nhibernate.dll assembly was added to your current project. Uninstalling a NuGet Package Type in Uninstall-Package Nhibernate and after brief pause, you'll see a confirmation message similar to the following: Successfully removed 'NHibernate 3.2.0.4000' from MyApplication. Successfully uninstalled 'NHibernate 3.2.0.4000'. The following package elements are also removed:
  • References in the project. In Solution Explorer, you no longer see the library in the References folder or the bin folder. (You might have to build the project to see it removed from the bin folder.
  • Files in the solution folder. The folder for the package you removed is deleted from the packages folder. If it is the only package you had installed, the packages folder is also deleted.)
  • Any changes that were made to your app.config or web.config file are undone.

Graphical Installations
As explained earlier, clicking Manage Nuget packages…, from the context menu for either a Solution Explorer project or a project's References folder or via the Project menu opens the Manage Nuget packages window. This window will allow you to add a reference a NuGet package in your project.

To the left of the window are a few accordian folders to help you find packages that are either on-line or already installed.  Just like the previous section, I'll assume you are installing Nhibernate for the first time, so you would select the Online folder and click All. After waiting for package descriptions to download, you'll notice that there are too many to scroll through in a short period of time.  Therefore, use the search box located at the top right corner of the window and type Nhibernate as I've done in the previous figure. You'll see Nhibernate appear in the list. Click the Install button on the Nhibernate entry. If the installation was successful, you'll see a message box display and disappear quickly (or maybe not if your machine is very fast or you blink at that moment).

Then you'll see a reference to the Nhibernate.dll assembly in your project's references list. If you open the Manage Nuget packages window again, you'll see Nhibernate listed in the Recent packages folder.



Summary
You can install NuGet via the on-line home page with a click of a button.  Nuget provides two ways to work with packages, via console or graphical window.  While the graphical window is easiest, the console window is more powerful. You can now quickly add project references to many available packages via the NuGet service.

No comments:

Post a Comment