如何处理 Nuget 依赖程序集的相对 HintPath?

How to deal with relative HintPath for Nuget dependency assemblies?

This issue describes in more detail and discusses the problem I was describing in a recent question - 一个项目在多个解决方案中,<HintPath> 是本地包目录。因此,根据正在构建的解决方案,它 运行 会导致依赖性混淆问题。

有人知道好的解决方法吗?在多个解决方案中拥有同一个项目并不少见,而本应有助于避免依赖地狱的 Nuget 却依赖于脆弱的本地路径,这似乎很疯狂。

Does anyone know of a good workaround? It can't be unusual to have the same project in multiple solutions and it seems crazy that Nuget, which is supposed to help avoid dependency hell, is relying on fragile local paths.

我认为您可以更改 nuget.config 文件中的 repositoryPath path,以便提示路径将使用全局 nuget 缓存下的包,而不是再次将包复制到您的解决方案文件夹下。这减轻了 nuget 包的依赖性。

打开C:\Users\xxx(current user name)\AppData\Roaming\NuGet\NuGet.Config然后在其中添加这些:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="repositoryPath" value="%USERPROFILE%\.nuget\packages" />
</config>

</configuration>

然后重新启动 VS,它将直接从全局 nuget 缓存中引用包。

然后在工具下运行update-pacakges -reinstall-->Nuget包管理器-->Package Manager Console 使用新的 Hintpath.

除了,使用PackageReference nuget management format将避免这种行为,你不会被复杂的Hintpath问题所困扰。这是一种新的 nuget 包管理格式,更容易。

右击 packages.config 文件--> migrate packages.config to PackageReference.

并且注意当你迁移到构建服务器时,如果你只是通过命令行构建它,你应该运行 nuget restore xxx.sln来恢复这些包裹。参见 this link