What is NuGet?
NuGet is the package manager for the Microsoft development platform including .NET. The NuGet client tools provide the ability to produce and consume packages. The NuGet Gallery is the central package repository used by all package authors and consumers.
Visual Studio 2010, 2012 and 2013
For Visual Studio 2010, NuGet is available through the Visual Studio Extension Manager. The extension can be installed into the Professional, Premium, and Ultimate editions. Additionally, the extension can be installed into Visual Web Developer 2010 Express
Starting with Visual Studio 2012, NuGet is included in every edition (except Team Foundation Server) by default. Updates to NuGet can be found through the Extension Manager.
To check if your copy of Visual Studio already has the NuGet extension, look for Library Package Manager in the Tools menu of Visual Studio. If your copy of Visual Studio does not already have the Library Package Manager (NuGet) extension, you can install it using the Extension Manager.
NuGet Package Manager
When we have to add an existing package in our .Net project, we first need to make sure that the required package does exist and we can install it in our project. To search the existing packages, we need to look by using Package Manager Console.
In the menu, go to Tools and expand NuGet Package Manager, then select Package Manager Console.
This will show you a console window in the bottom of the screen. Following is the screenshot of Package Manager Console.
This console manager initially has the Package Source as nuget.org and Default Project as the current project.
Most solution providers host their solution’s NuGet packages on their Websites. First we need to add the vendor Website as Package source. For this purpose we use the Package Manager Settings dialog. In the menu, go to Tools and expand NuGet Package Manager, then select Package Manager Settings.
By using this setting dialog, we can add, remove or update any existing NuGet package source. We cannot update or remove the first package source of nuget.org. After adding our required NuGet source we can see that the newly added package source is available in our Package Source dropdown in the Package Manager Console.
Search for a Package
In the Package Manager Console, we can search existing packages by using the following command;
Get-Package – ListAvailable
This command will show the list of all packages available in the Package Source. If we select All in Package Source, then this will show all packages from all package sources. Following is an example of the search result.
The search result normally shows three columns: Name of the package, Package Version and Package Description.
If we know complete name of our required package or some part of its name, then we can minimize search result and find the required package. We can use the following command for this purpose;
Get-Package –ListAvailable ABCpdf
The aforementioned command will provide the following result. Here ABCpdf is our sample NuGet Package name.
Install a NuGet Package
To install an existing NuGet package we have to use the following command;
This command will download the required package from the source. It will also create a folder named “Packages” in the project folder and add the reference of assemblies in the project.
If the package you are installing is dependent on other packages, NuGet installs them as well, if they are not already installed.
If the package requires license acceptance, you will not be prompted in a dialog box. Instead, a message states that your use of the library constitutes license acceptance. In the aforementioned example, the package we installed in our project gave the following message and completed the installation.
“You are downloading ABCpdf from WebSupergoo, the license agreement to which is available at http://www.websupergoo.com/license.htm. 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.”
If installation of new package requires to edit the web.config or app.config, then Install command also perform this update in the configuration files of the project.
Update an existing Package
Consider the following scenario: a solution provider delivered a specific NuGet package to be used in our solution and after some time they release a new version. In this case, we want to edit the complete project and remove the old reference and add the new ones. For this, NuGet Package Manager Console also provides a command. We can use the following command to update an existing package in our solution. This will not only download the updated version from the package source but it will also update the all references in the solution.
Uninstall a Package
If we want to remove any package which we added in our project, we can use the following command:
This command will not only remove the reference of the added assemblies from the project, it will also delete the packages folder which was created in the process of installation.