Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NuGet when sharing projects between solutions

I have several solutions that share certain common projects, such as one that contains all the necesary things to access my data model.

Solution Explorer situation

The actual problem is that trying to install packages with NuGet in one proyect with dependencies (ex. Iesi.Collections), creates a reference to the dll in the current solution directory and not in the proyect directory. Then, when I try to go to another of my solutions, the shared project fails to compile (because of the missing assembly).

First path reference Second path reference

I have searched all around for using NuGet in shared projects between solutions in an independent way.

So the question is... How can I manage obtain an independent behaviour and references between solutions in NuGet?

like image 329
piraces Avatar asked Jan 23 '26 22:01

piraces


2 Answers

An alternative approach to this problem is modifying package reference path in .csproj file like by utilising $(SolutionDir) macro - this will point the reference to correct directory in both solutions:

<ItemGroup>
    <Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
        <HintPath>
            $(SolutionDir)packages\Newtonsoft.Json.10.0.3\lib\net40\Newtonsoft.Json.dll
        </HintPath>
    </Reference>
    ...
</ItemGroup>
like image 157
David Indra Avatar answered Jan 26 '26 12:01

David Indra


One way to solve this issue is to add a .nuget\NuGet.config file to all solutions, and set the repositoryPath to a decicated local folder.

From this link, assuming you're using VS2015:

  1. Create a .nuget folder in the root of the solution (on the file system) (if you're having trouble creating a .nuget folder using windows explorer, you can do so by entering .nuget. instead)

  2. Inside that folder, create a file NuGet.config.

  3. In Visual Studio 2015, right click on the solution and add a new solution directory called “.nuget”

  4. Right click on that folder and select to add a new an existing file and select the NuGet.config file created in (2).

  5. Add content like this inside the NuGet.config file:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <solution>
    <add key="disableSourceControlIntegration" value="true" />
  </solution>
  <config>
    <add key="repositoryPath" value="..\..\..\..\NugetPackages" />
  </config>
</configuration>
  1. Restart Visual Studio

Note that you'll have to fix existing references, for instance by removing and then adding all packages again.

I personally use an absolute path for the packages, since not all of my solutions are in the same folder hierarchy - if you're in a team you might want to discuss this first.

like image 34
C.Evenhuis Avatar answered Jan 26 '26 14:01

C.Evenhuis