Reputation: 517
I have a complex software solution that I wanted to separate in several Visual Studio Solutions for greater ease, but it all resulted in a mess, because all the assembly references got broken. I inspected .csproj files and found that the assembly references were right, pointing to the correct file system location, so I didn't understand what was the problem.
So I tried to copy the NuGet "packages" folder to a location nearest to the csproj file, and updated the HintPath to point to the new location and... It magically got fixed!
For example:
This does not work:
<HintPath>..\..\packages\Microsoft.AspNet.WebPages.3.2.7\lib\net45\System.Web.WebPages.Razor.dll</HintPath>
This works fine:
<HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.7\lib\net45\System.Web.WebPages.Razor.dll</HintPath>
Has anybody else experienced this behavior? Which is the correct way of structuring solutions that share common projects without falling in this annoying problem? Is there any best practice?
Update: I have experienced the same problem again and now the solution has been get back to the original HintPath value and unload and load the project again. I don't know what is the cause, but now the projects compile successfully again. Perhaps Visual Studio has any cache for the paths to the assemblies and gets crazy when the projects are moved to another folder?
Upvotes: 3
Views: 1509
Reputation: 21568
The 'HintPath' is added to your .csproj file when you reference a Nuget package. As the name suggests, it is a hint to the compiler telling it where to look for the assembly in the form of a relative link to your Nuget Packages folder.
It is also - as you have discovered - an annoyance, as if you move or check out the project at a different filesystem depth the path is broken.
The easiest way to resolve this is to move from packages.config to the newer PackageReference package management format. This records your Nuget references as package references rather than assembly references, and removes all 'HintPath' entries.
You can enable this in the Nuget Package Manager options in Visual Studio. The following screenshot is from Visual Studio 2019:
Copy both of my settings from "Package Management".
To migrate an existing project, right-click on the References node or the packages.config file in Solution Explorer and select "Migrate packages.config to PackageReference" [full instructions].
If for some reason the above is not feasible (e.g. you are using an old version of Visual Studio), there is an msbuild script called HintPathOverwrite
available on GitHub and Nuget which can dynamically change the HintPath at compile time.
Upvotes: 5