NuGet – Sharing code in Visual Studio 2010

It’s an open source visual studio 2010 extension that lets you include other peoples code in your projects, or your own if you package it.

The extension handles the grunt work of downloading, adding references, and configuring packages you choose to add to your project. You can also easily remove packages you’ve installed.

The extension is installed automatically with MVC 3 or you can get it here.

How do I include a package in my project ?

Right click references and choose a package from the list.

Add Package Reference

Or, using the package manager console (ctrl w, ctrl z). You need Powershell 2 installed which is part of the Windows Management Framework Core package.

Package Manager Console

Try some of these commands:

  • List-Package
  • Add-Package Automapper (or nap Automapper)
  • Update-Package Automapper
  • Remove-Package Automapper
  • New-Package myPackage -Spec myPackage.nuspec

See help page for a full list of commands and syntax.

Changes and references are kept local to the project your working with and all the downloaded files are put in a folder called \packages under your project.

Packages are not automatically updated, you get to control that. In fact once added it’s transparent to visual studio, as if you added the files and configuration by hand.

Where does my list of packages come from ?

The list of packages available is taken from feeds you configure as Visual Studio options.

Settings

Feeds are easily to setup, no server required, just create a directory that can be accessed and place all packages(*.nupkg files) in it, then add it to the available package sources, its that simple.

How do I create a package ?

You need to create a spec file then build a package using the new-package command in the package manager console.

The spec file includes the metadata and dependencies for the package and you don’t need to include a file list if you use the conventions specified in the documentation on building a package.

  • tools – The tools folder of a package will be added to $env:Path (PATH) when the package is installed
  • content – These files will be copied to the site root on install
  • lib – dlls under this folder will become assembly references on install,  root lib folder is short-hand for specifying any framework
    • lib/40 –> .NETFramework 4.0
    • lib/SL4 –> Silverlight 4.0
    • lib/net20 –> .NETFramework 2.0

Spec files (*.nuspec) are xml files, so use your editor of choice.

myPackage
1.0.0
Me



To update a config file e.g. web.config include a web.config.transform file in the package with the sections you want to add.

See the help page for more.

Links

Bil Simser
Scott Gu
Haacked
Scott Hanselman