NuGet for solutions with multiple projects

.NetNuget

.Net Problem Overview


Suppose I have a solution with 3 projects:

  • Core
  • UI
  • Tests

Some of the NuGet packages I use will apply to all 3 projects. Some will just apply to UI and Tests, and some will just apply to Tests (like NUnit).

What is the right way to set this up using NuGet?

  1. Should I use "Add Library Package Reference" on all three projects any time I need a reference?
  2. Should I use "Add Library Package Reference" the first time I need a package, and then use Add Reference->Browse for subsequent usages?

In either case, how many packages.config files should I have?

.Net Solutions


Solution 1 - .Net

For anybody stumbling across this, now there is the following option :

> Right-click your solution > Manage NuGet Packages for Solution...

... Or:

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

And if you go to the Installed packages area you can 'Manage' a single package across every project in the solution.

Solution 2 - .Net

Use the console to target multiple projects

Tools > Library Package Manager > Package Manager Console

then use this command

Get-Project PROJECT-NAMES-WITH-COMMAS | Install-Package PACKAGENAME

for example

Get-Project Core,UI | Install-Package FluentDateTime

Solution 3 - .Net

This sweet deal works for me:

PM> Get-Project -all | where {$_.Name -match "Songhay.Silverlight" -and
    $_.Name -notmatch "ApplicationLoader" -and $_.Name -notmatch ".Xml"}
    | ForEach-Object {Install-Package MvvmLight -project $_.Name}

Solution 4 - .Net

If you want to install a package across multiple solutions I wrote a handy Powershell script for doing it, see here.

You can even filter the Get-Project -All command and target a sub-set of the project list.

Solution 5 - .Net

You should use the "Add Library Package Reference" for all your external library on every project in your solution. You'll end up with a packages.config per project.

However, you'll download the package only one time and reuse them locally for all your other projects.

Solution 6 - .Net

In Package Manager Console you can write the following command:

Get-Project -all | ForEach-Object {Get-Package -ProjectName $_.Name -filter 
PACKAGE_NAME} | where-object { $_.id -eq 'PACKAGE_NAME' } | Install-Package 
PACKAGE_NAME -Version VERSION

You can use that command for install or update as well (Update-Package)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPaul StovellView Question on Stackoverflow
Solution 1 - .NetJasonView Answer on Stackoverflow
Solution 2 - .NetSimonView Answer on Stackoverflow
Solution 3 - .NetrasxView Answer on Stackoverflow
Solution 4 - .NetAaron PowellView Answer on Stackoverflow
Solution 5 - .NetNekreshView Answer on Stackoverflow
Solution 6 - .NetDark_KnightView Answer on Stackoverflow