My friend and fellow DevelopMentor instructor, Michael Kennedy, just turned me on to the NuGet Package Manager, a very cool way to install and use open source software right from within Visual Studio. Let’s say you use one or more open source projects, such as my Simple MVVM Toolkit or NHibernate. Instead of manually downloading and installing the software, if you’ve installed NuGet (from the Extension Manager) you can open a Visual Studio project, right click on references and select “Add Library Package Reference.” NuGet will add the required references, and also references to all dependent assemblies (including other open source projects), as well as additional content, such as templates, code snippets and transforms to app.config or web.config. Scott Hanselman has a nice article on NuGet (back when it was still called NuPack), and Michael Kennedy has a list of killer open source projects he found on the NuGet Gallery.
NuGet uses the xcopy model or deployment, as opposed to the msi installer model. That means, instead of referencing assemblies from the Global Assembly Cache (GAC), each application has its own private copy of referenced assemblies. This means you can install a new version without affecting apps using an older version (a problem sometimes referred to as DLL Hell). Packages installed with NuGet are scoped at the level of the Visual Studio project. So everything you need is installed right there. A package can target more than one version of the .NET Platform, and NuGet will install the correct version for the Visual Studio project’s target platform.
To get started with NuGet you should install it via the Extensions Manager dialog from within Visual Studio (Tools / Extension Manager).
After you’ve got NuGet, you can right-click on References and you’ll see an open to “Add Library Package Reference,” at which point you’ll be presented with a dialog that lets you search for open source projects on the NuGet Gallery. For example, searching for the “mvvm” tag you’ll find three packages for Simple Mvvm Toolkit: Silverlight, WP7 and WPF.
After clicking Install and closing the dialog, you’ll see that required references have been added.
As well as content files. For example, the package for my Simple MVVM Toolkit adds a readme.txt file, templates, code snippets, source code, and a small sample app.
Instructions in the readme file will explain how to add the code snippets and Visual Studio item templates. If you’ve already done this, you can simply delete them from the project. And if you want to, you can unzip and add the source code project (under source), add it to the solution, and add a project reference to it.
Unfortunately, NuGet does not perform automatic updates to packages. So if you want to see if there is a new version, you need to right-click on the References node, select “Add New Package Library Reference,” then click the Updates node on the left to see if there is an update to the packages you’ve installed in the current Visual Studio project. If so, just click the Update button and both the references and content files will be replaced.
I spent the better part of the weekend figuring out how to contribute packages to the NuGet online gallery. Phill Haack has a great post on how to do this. Here are the steps I followed:
1. Install NuGet from CS Tools/Extension Manager
2. Install NuGet command line
– Create a new VS project, right-click on References,
select Add Library Package Reference
– Search for nuget, select NuGet.CommandLine and install
– Right-click on the project and select Open Folder in
– NuGet.exe to where it makes sense – for ex:
– Edit Path environment variable to add the folder path
> Windows start menu, right-click computer, properties,
advanced system settings, environment variables,
select PATH system variable to edit, paste ; then path
– Should be up-to-date, but next time running the cmd line
you can type: nuget update to install updated version
3. Create a folder to contain package
– Add libs folder for binaries
> Can contain framework-specific folder, for ex, net40, sl4
> Presently there is no client profile support, so you need
a separate package for wp7 or other client profiles
– Add content folder for other files (snippets, templates, etc)
– Add xxx.nuspec file with xml like so:
<?xml version="1.0" encoding="utf-8"?>
<description>What your project is all about</description>
4. Open a cmd prompt, go to the dir where nuspec file, lib & content
are located, then enter following:
nuget pack mygreatstuff.nuspec
– you should get a .nupkg file in that location
5. Create an account at nuget.org
– They will need to manually approve your registration
– Click Get Started under Add New Package
– Go through the steps to add the package
I also spent quite a bit of time writing a good old fashioned batch file with commands to copy files into the package structure. Now that I’ve got it all automated, building NuGet packages is a breeze.
Pingback: Robb Sadler's Weblog
Pingback: Tony Sneed’s Blog -> Michael C. Kennedy’s Weblog -> Facebook C# SDK « Robb Sadler's Weblog