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.
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:
There are two ways you can use the Library Package Manager in Visual Studio to work with packages:
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:
In the Search Online box type “logging” to search for available libraries with enhanced logging functionality.
Use the paging links at the bottom of the results window to scroll through the packages until you find the Elmah package. 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.
Select the elmah package and then click Install.
After the installation, completes, click Close.
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.
The following screen shot shows the Elmah assembly in bin, and Intellisense working when you type “Elmah.” in a Razor code block.
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:
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:
To remove a package using the package manager console:
Close the package manager console.
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.