Friday, February 24, 2012

Using NuGet to Install Packages Across a Solution


Earlier today I created a new Unit Test project in an existing VS solution. I then wanted to install the NUnit package to the project using NuGet - but I want the same version of NUnit that is used on all the other Unit Test projects in the solution.

A new version of NUnit just came out, and all the existing projects use the old version. And the NuGet GUI would only let me install the latest package, not previous versions.

So what to do?

Ofcourse at some point in a codebase you'll want the latest version, but until you are ready to upgrade a package across every project in your solution, you'll want to install older package versions, to keep all your solution packages in sync.

The short answer is download the latest version of NuGet! Version 1.6 is out now, and since 1.5 has had the new feature:

Tools >
Library Package Manager >
Manage NuGet Packages for Solution...

You can see which version of NuGet you currently have by checking the help dialog, or by using the Visual Studio Extension Manager. From the latter you can also uninstall your old NuGet and download and install the new one.

The Longer Answer
The longer answer, which highlights a little more about how NuGet manages your packages behind the scenes, is this:

  1. First, check which version of your package you have in your existing project references by looking at it's property sheet:
Property sheet
  1. Then launch the Package Manager Console and type in:
Install-Package <Package-Name> -Version <Version-Number>


Install-Package NUnit -Version
  1. NuGet should discover that your package is already installed elsewhere in your solution, and simply reference the existing package (as shown by the output below).
PM> Install-Package NUnit -Version
'NUnit' already installed.
Successfully added 'NUnit' to My.Project.Tests.

The 'packages' Folder
For more clarity, it's a good idea to browse to the 'packages' filesystem folder at the root of your solution. It contains a subfolder for each package you have installed in any project in your solution. The NuGet Package Manager ensures that when you use the same package across different projects, they share a single reference to the appropriate subdirectory. This keeps things neat and tidy in a way that we used to have to do by hand.

And ofcourse you can upgrade NuGet, do it the easy way, and let the Package Manager do it for you!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.