在 Visual Studio 2015 解决方案中还原所有 Nuget 包 [英] Restoring all Nuget Packages in a Visual Studio 2015 Solution

查看:114
本文介绍了在 Visual Studio 2015 解决方案中还原所有 Nuget 包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多关于 SO 和 NuGet(以及整个互联网,真的)的答案,但我似乎无法克服我在 Visual Studio 2015 中使用 NuGet 包还原遇到的问题.我有以下内容场景.

解决方案 A 结构

--项目A

如果我打开并构建解决方案 A,我会看到显示 nuget 包还原进度和解决方案构建成功的对话框.

解决方案 B 结构

--项目A

--项目 B

但是,假设我从来没有构建解决方案 A(即从 TFS 的新拉取),如果我打开并构建解决方案 BI,看到显示 nuget 包恢复进度的对话框,但构建失败,因为项目 A 无法构建.

似乎正在发生的事情是 NuGet 正在恢复项目 B 的包,而不是项目 A,因此构建失败.就这一点而言,如果我查看项目 B 的引用,所有 NuGet 引用都已解析,但项目 A 的引用仍然损坏.

几点:

  • 我已禁用 NuGet 的源代码控制集成,因此我没有检查 Packages 文件夹.
  • 每个项目都有自己的packages.config文件
  • 解决方案 B 中的构建顺序是项目 A,然后是项目 B

想法将不胜感激.

解决方案

默认情况下,NuGet 在解决方案根目录中创建解决方案的 packages 文件夹,并且每个项目都将其包 DLL 引用到该本地"包文件夹.在您的示例中,如果您打开项目 A 的 .csproj 文件,您可能会看到引用路径类似于 ..\packages\[package name]\[etc].>

因此,当您从 TFS 重新提取并构建解决方案 B 时,项目 A 找不到它的 DLL,因为 c:\workspace\Solution A\packages 尚不存在(或其他任何绝对路径在您的机器上).

要更正此问题,请使用在 c:\workspace\packages 中创建的共享包文件夹.为此,您必须在每个解决方案中向 NuGet.config 添加一个附加节点(请参阅 https://docs.nuget.org/consume/nuget-config-file 了解详情;我还假设您在 c:\workspace\Solution A\.nuget):

<add key="repositorypath" value="..\..\packages"/></config>

我在这里使用了相对路径,但您也可以使用绝对路径,并且文档说您也可以使用 %HOME%.

执行此操作,然后重新启动 Visual Studio.下次打开包管理器时,它应该询问您是否要恢复丢失的包,假设您单击是,它会将它们放在新位置.最后一步是编辑 .csproj 文件并将 ..\packages 的所有实例更改为 ..\..\packages(或者您可以卸载并重新安装包,但我发现编辑 .csproj 的速度要快得多).

I have read many of the answers on SO and NuGet (and the Internet in general, really), but I can't seem to overcome the problem I am having with NuGet package restore in Visual Studio 2015. I have the following scenarios.

Solution A Structure

--Project A

If I open and build Solution A I see the dialog box that shows the nuget package restore progress and the solution builds successfully.

Solution B Structure

--Project A

--Project B

However, assuming that I have never built Solution A (i.e. fresh pull from TFS), if I open and build Solution B I see the dialog box that shows the nuget package restore progress, but the build fails because Project A fails to build.

What appears to be happening is that that NuGet is restoring the packages for Project B, but not for Project A thus the build failure. To the point, if I look at the references for Project B all of the NuGet references have resolved, but the references for Project A are still broken.

A few points:

  • I have disabled source control integration for NuGet so I am not checking in the Packages folder.
  • Each project has its own packages.config file
  • The build order in Solution B is Project A then Project B

Thoughts would be greatly appreciated.

解决方案

By default, NuGet creates the solution's packages folder in the solution root, and each project references its package DLLs to that "local" packages folder. In your example, if you open the .csproj file for Project A, you'll probably see that the reference path is something like ..\packages\[package name]\[etc].

So when you do a fresh pull from TFS and build solution B, Project A can't find its DLLs because c:\workspace\Solution A\packages doesn't exist yet (or whatever the absolute path is on your machine).

To correct this, use a shared package folder, created at c:\workspace\packages. To do this, you have to add an additional node to the NuGet.config in each solution (see https://docs.nuget.org/consume/nuget-config-file for details; I am also assuming you have a NuGet folder at c:\workspace\Solution A\.nuget):

<config>
  <add key="repositorypath" value="..\..\packages" />
</config>

I used a relative path here, but you can use an absolute path as well, and the documentation says you can use %HOME% as well.

Do this, then restart Visual Studio. The next time you open the package manager, it should ask you if you want to restore missing packages, and assuming you click yes, it will put them in the new location. The last step is to edit the .csproj file and change all instances of ..\packages to ..\..\packages (or you can uninstall and reinstall the package, but I find editing the .csproj a lot faster).

这篇关于在 Visual Studio 2015 解决方案中还原所有 Nuget 包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆