Monday, December 14, 2015

NuGet Package Manager Introduction


NuGet is a popular .Net package manager.  .Net was late to the party for package managers (see RubyGems and apt-get), but it's here and been gaining traction the last few years.

 NuGet relieves the developer from searching the web for common libraries and provides a single place to download most libraries.  NuGet also has a common file structure so it helps to standardize the file structure across projects.

Adding a Library

To add a package to a project, right-click on the project and select "Manage NuGet Packages..."
(If you don't see that on the context menu, you may need to download the NuGet extension on older versions of VS).


Set the source to "Online" and search for your package.  Let's install Json.NET.


At this point the Package Manager does the following:
  1. Creates a solution\packages directory and a "repositories.config" file if not already there

  2. Downloads, typically from nuget.org,  the *.nupkg file into the solution\packages directory and unzips contents including the dlls needed.

  3. Adds a "packages.config" file to project if not already there



  4. Adds references in the project to the libraries in the solution\packages directory


  5. Updates the web.config if needed

Json.Net is now a part of our project.

Removing a Project

The VS GUI for NuGet will also let you uninstall a package.  Removing a package should remove references, package.config entries, but may not remove web.config settings or dependencies.  Always check to make sure all items are removed.

The Package Manger Console

To access the command-line go to Tools/NuGet Package Manager/Project Manager Console

Here are a few common powershell commands for the console
  • PM> Get-Package # gets all packages in solution or project
  • PM> Get-Package -ListAvailable Elmah # all available named Elmah
  • PM> Install-Package elmah # install elmah and all its dependencies
  • PM> Uninstall-Package elmah #uninstalls library, but not necessaily its dependencies
  • PM> Uninstall-Package elmah.corelib # uninstalls dependency
  • PM> Install-Package jquery -Project MyProject3 # install in specific project regardless what dropdown says
  • PM> Install-Package jquery -Version 1.5 # not in GUI
  • PM> Get-Package -Updates # show updates available
  • PM> Update-Package jquery # updates all packages in solution regardless of scope dropdown
  • PM> Update-Package jquery -Version 1.5 -Project MyProject2 #changes version just for a specific project
  • PM> Update-Package -reinstall jquery -Project MyProject2 # re-adds references
More command-line docs at docs.nuget.org

Version Control

We check in our "packages.config" file in each project to our version control system, Perforce. But sometimes Perforce does not see that a "packages.config" file has changed and we manually add it to Perforce.

We don't checkin any of the packages in our packages directory, and rely on the build system to grab the missing libraries from the NuGet repository. Also the "packages/repository.config" is not checked in, but rebuilt each time.



No comments: