Have you ever noticed how complex it can be to add, remove, and update libraries of code functionality in Visual Studio?  First you have to hunt all over the place to find the library you want.  Then you have to download it, and figure out how to install it.  Often, you find it has dependencies, which then means finding, downloading, and installing other libraries too.  After everything is installed and working, suppose one of the libraries you installed gets updated.  How will you find out about the update? How will you install it?  And if there are dependencies among libraries, how will they be affected?  Finally, when it’s time to remove a library, unless the library and its dependencies were installed as an integrated package, you get to experience the joy of reversing the installation process and removing each library.  As you know if you’ve done this, working with libraries can quickly become painful.

Microsoft has released some new tools to streamline the process of finding, adding, and managing libraries and add-on packages in Visual Studio.  NuGet is an open source package management system based on the .NET Framework.  The NuGet application has been added into Visual Studio in the form of the Library Package Manager. In this post, I’ll demonstrate how to use the new Library Package Manager to install and remove a library in Visual Studio 2010.  This also works in Visual Web Developer 2010 Express, which is what I’ll use here.

Installing the Library Package Manager

You can install the Library Package Manager by downloading it directly as a Visual Studio extension (*.vsix file) from the NuGet downloads page, or you can install the latest Web Platform Installer and then install the ASP.NET MVC3 Release Candidate.  Here we’ll use the second approach.

To install the Library Package Manager in Visual Studio:

  1. Run the latest release of Web Platform Installer (version 3.0).  You may have to click Allow in your browser one or more times to enable the Web Platform Installer to load.
  2. Click Install to start installing the ASP.NET MVC3 Release Candidate. 
    pm-0 

 

Working with Libraries and Packages

There are two ways you can use the Library Package Manager in Visual Studio to work with packages: 

  • The package manager window.  This is a simple UI window for adding, removing, and updating packages.
  • The package manager console.  This is a console-type window in which you can enter Powershell commands for adding, removing, and updating packages.

Using the Package Manager Window

In this section we’ll show how to use the package manager window to manage packages in Visual Studio.

To add a package using the package manager window:

  1. Start Visual Studio (in this post, I am using the free edition, Visual Web Developer 2010 Express).
  2. Click New Web Site, and then create a new site or project.  In this post I will create an ASP.NET web site (Razor) to use with the package manager.
    pm-0a
  3. After the site is created, right-click the node for the site in Solution Explorer, and then click Add Library Package Reference.
    pm-1
  4. In the Add Library Package Reference window, click Online to display the list of available online packages.
    pm-2 
  5. In the Search Online box type “logging” to search for available libraries with enhanced logging functionality.

  6. Use the paging links at the bottom of the results window to scroll through the packages until you find the Elmah package. 
    pm-3
    Note:  This is a good example of how the Library Package Manager helps you find useful packages.  If you didn’t know ahead of time that you wanted the Elmah logging package, you can search more generally to find a list of packages that supply the features you want.

  7. Select the elmah package and then click Install.

  8. After the installation, completes, click Close

  9. You can see the Elmah assembly appears in the bin folder in Solution Explorer.  To demonstrate that you can now use the library, add the following block of Razor syntax and type Elmah. to see that Intellisense recognizes the library. 

    @{
         Elmah.
    }

    The following screen shot shows the Elmah assembly in bin, and Intellisense working when you type “Elmah.” in a Razor code block. 


    pm-4
    After you see the Intellisense working with the Elmah library, remove the code block you just added. 

 

Now that you’ve seen how to add a package, let’s remove the same package.

To remove a package using the package manager window:

  1. In Solution Explorer, right-click the node for the site, and then click Add Library Package Reference.
  2. Click Uninstall on the elmah package in the Add Library Package Reference window.
    pm-5
  3. Close the window.  The Elmah assembly has been removed from the bin folder in Solution Explorer.
    pm-6

Using the Package Manager Console

We’ve seen how to add and remove the Elmah library in a Visual Studio project, using the package manager window.  Now we’ll go through the same process using the package manager console.  This console gives you the ability to manage packages in a more powerful way, using Windows Powershell commands.

To add a package using the package manager console:

  1. In Visual Studio click Tools | Library Package Manager | Package Manager Console.  The package manager console window appears.  If you click the drop-down icon in the upper right corner of the window, you can select whether to display it as a floating or a docked window, or as a tabbed document.
    pm-7
  2. To see the list of available packages in the console, at the prompt type list-package and then press Enter.  
    pm-8
  3. If you are new to Windows Powershell command, at the prompt you can type get-help * and then press Enter to display a list of available commands.
     pm-9
  4. To get help on a particular command, type get-help [command].  To display help for the install-package command, type get-help install-package and then press Enter. 
    The syntax and options for the install-package command are displayed.
    pm-10
  5. To install the Elmah package, type install-package elmah and then press Enter.  After the installation completes, the Elmah assembly again appears in the bin folder in Solution Explorer.
    pm-11
  6. Optionally, you can add a block of code in the editor and type “Elmah.” as you did in the previous section, to demonstrate that the library is recognized by Intellisense.
    After you establish that Intellisense recognizes the Elmah library, remove the test block of code.

Now that you’ve seen how to add a package, let’s remove the same package.

To remove a package using the package manager console:

  1. To remove the Elmah package, at the prompt type uninstall-package elmah and then press Enter.  After the removal completes, the Elmah assembly is no longer visible in the bin folder. 
    pm-12 
  2. Close the package manager console.

 

Summary

You’ve seen how you can use the Library Package Manager in Visual Studio to find, add, and manage libraries and packages.  You can use the package manager window, or the package manager console, to do the same basic tasks.  In addition to adding and removing packages, you can also use these tools to update a package.  When updates are available, they are automatically displayed for your installed packages when you display them, and you can simply click an Update button (in package manager window) or run an update-package [package name] command (in package manager console) to update a package.  The nice thing is that this updates both a package and its dependencies if needed.

You can also use the NuGet package management application to create packages of the type you installed here.  For information on creating basic packages, see Creating a Package at the NuGet project.