Καλώς ορίσατε στο dotNETZone.gr - Σύνδεση | Εγγραφή | Βοήθεια

Everyday's Software Design

Scribbles on Software Engineering

Φεβρουάριος 2012 - Δημοσιεύσεις

Small Introduction to NuGet

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

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

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 thefile through the browser's download manager.  Regardless of how youreceive the NuGet installer, execute the downloaded NuGet.Tools.vsix toinstall Nuget into visual Studio.

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


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

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 istext-based and you'll need to type in commands to work with packages.


In this post, I'll explain how to use the PackageManager Console to install Nhibernate, but there are many morecommands, explained in the NuGet PackageManager Console Commands documentation.  To install Nhibernate,open your current project where you want Nhibernate installed, and type thefollowing 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 assemblywas added to your current project.

Uninstalling a NuGet Package
Type in Uninstall-Package Nhibernate and afterbrief 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. (Youmight have to build the project to see it removed from the bin folder.
  • Files in the solution folder. The folder for thepackage you removed is deleted from the packages folder. If it is the onlypackage you had installed, the packages folder is also deleted.)
  • Any changes that were made to your app.config orweb.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'sReferences folder or via the Project menu opens the Manage Nugetpackages window. This window will allow you to add a reference a NuGetpackage in your project.


To the left of the window are a few accordian folders tohelp you find packages that are either on-line or already installed.  Justlike the previous section, I'll assume you are installing Nhibernate for thefirst time, so you would select the Online folder and click All
After waiting for package descriptions to download, you'llnotice 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 andtype Nhibernate as I've done in the previous figure. You'llsee Nhibernate appear in the list.
Click the Install button on the Nhibernateentry. If the installation was successful, you'll see a message box display anddisappear quickly (or maybe not if your machine is very fast or you blink atthat moment). Then you'll see a reference to the Nhibernate.dll assemblyin your project's references list.
If you open the Manage Nuget packages windowagain, you'll see Nhibernate listed in the Recent packages folder.


Summary
You can install NuGet via the on-line home page with a clickof a button.  Nuget provides two ways to work with packages, via consoleor graphical window.  While the graphical window is easiest, the consolewindow is more powerful. You can now quickly add project references to manyavailable packages via the NuGet service.