Marshall
Marshall

Reputation: 275

How to set dynamic path to nuget references?

Here is the sample project structure:

{ ProjectA }
    { packages } <-- packages are created here
    { ProjectA }
        - ProjectA.csproj <-- references ProjectB and C.
        - packages.config
    - ProjectA.sln <-- contains all projects: A, B and C.

{ ProjectB }
    - ProjectB.csproj
    - packages.config

{ ProjectC }
    - ProjectC.csproj
    - packages.config   

*{ packages} <-- *When I manually paste packages here. So one level above ProjectB.csproj file, then ProjectB compiles.

ProjectA solution has all three projects: A, B and C. ProjectA reference ProjectB and ProjectC.

When I compile ProjectA (projects B and C are compiled as well), all nuget packages are downloaded into {packages} folder on the same level as solution file. The problem is that ProjectB is not compiling. Yes... only ProejctB. I'm not even gonna investigate why only one project compiles although their configuration is exactly the same. Anyway...

In both ProjectB and C, when I expand References dlls from nuget are seen as they were missing (with the yellow rectangle). Somehow ProjectC compiles anyway, but ProjectB doesn't. The errors says it can't find the reference which is clearly in the packages folder.

My question is, how do I program/configure that sentance (psuedo code):

"Dear ProejctB, Please look for the references in the package folder generated on the same level as the solution file. The solution file, which is trying to compile you right now. Thank you"

PS. So technically, the path to the dll (reference) will be kinda dynamic. It will change depending on which solution file is opening/compiling the project. Is it possible?

Upvotes: 4

Views: 5483

Answers (3)

Krupa
Krupa

Reputation: 455

Nuget 3.x has packages.config concept and in this package name & version are mentioned at 2 place (In package.config and in .csproj file)

Reference in package config should be like this:

<package id="NewtonsoftJson" version="9.0.1" targetFramework="net46" />

Hint path in csproj should be like this:

<HintPath>..\packages\NewtonsoftJson.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>

Here "..\packages" says go one level up(means at solution level) and look for "packages" folder.

You should verify that hint path is exists or not. and Package version should be same (9.0.1) in both the files(package.config and .csproj)

As your Porject C compiles successful, it seems some issue in the packages which is used by only ProjectB.

If you are still facing issue, please provide below detail for further analysis.

  1. "package config"
  2. "ProjectB.csproj"
  3. msbuild compilation log, to know that in which package you are facing issue.

Upvotes: 1

Martin Ullrich
Martin Ullrich

Reputation: 100543

Since packages.config is slowly becoming deprecated, you could migrate your projects from packages.config to ProjectReference, where the NuGet packages are specified inside the csproj file and a shared global location is used to store the packages (and there aren't any references with HintPath that would need changing).

In VS 2017 version 15.7, there will be an option to migrate in the context menu of the references node (already available in the preview):

migrate option

PackageReference is already supported in VS 2017 since around 15.1 or 15.2, only the migration tool is in preview.

For new projects, VS 2017 (current version!) you can already select the default package reference style and allow for choosing it for new projects:

package reference style selection

Upvotes: 3

voytek
voytek

Reputation: 2222

The easiest way to fix it is by setting HintPath to:

<HintPath>$(SolutionDir)\packages\...

in .csproj files of ProjectB and ProjectC. It literally means: "look for the references in the package folder generated on the same level as the solution file. The solution file, which is trying to compile you right now"

This problem was reported multiple times. I believe it was fixed here. There is also NuGetReferenceHintPathRewrite, but I didn't test it.

Upvotes: 4

Related Questions