如何防止 UWP 项目在每次 Visual Studio 会话时恢复 NuGet 包?

How to prevent UWP projects from restoring NuGet packages upon each Visual Studio session?

为了在 Visual Studio 2015 年开发通用 Windows 应用程序,有没有人注意到每次使用您的解决方案启动新的 VS 会话时,第一步都必须恢复 NuGet 包构建过程?当没有对包配置进行任何更改时,这很烦人。因此,例如,如果您只需要快速重新启动 VS 和 运行 您的应用程序,它必须经过这个 20 秒的 NuGet 恢复,这似乎是不必要的。

有谁知道如何实现与桌面应用程序相同的行为(即它只在 NuGet 包丢失时恢复它们等)?

在 Visual Studio 中,按照以下 工具 菜单操作:

Tools -> Options -> NuGet Package Manager

然后,取消选中选项:

Automatically check for missing package during build in Visual Studio

这里的重要问题是:您是否想削弱您的开发工作(按照@user5525674 的建议,在构建期间禁用包恢复)并可能发生某些变化(例如在您的源代码控制中)并且没有正确构建?只是为了在 Visual Studio 会话的第一个构建中获得一些秒数?

它之所以有效是有原因的 'as is'。您可以阅读有关 NuGet v3 和构建过程的完整 documentation,但这是与问题相关的最重要部分:

The restore process runs ahead of the build itself, it then writes out a new file called project.lock.json. The file includes the package graph as well as other useful information about the packages restored. MSBuild reads the file and translates it into a set of folders where potential references can be found, and then adds them to the project tree in memory.

Where is this file dropped – Right next to the project.json

Should I check it in – Typically no, the file is included in the default .gitignore in Visual Studio normally not included in source control either through .gitignore or .tfignore.

回顾一下:

  • 您的解决方案下没有更多的包文件夹,下载的包缓存在您用户的 AppData 目录下。
  • 文件 project.lock.json 由还原过程生成并保存包引用(如果您在 VS 选项下禁用还原过程并且此文件丢失或引用被更改,这将使您的构建失败。
  • 引用 files/folders 由 MSBuild 保存在内存中 。一个新的 VS 会话意味着这棵树必须在内存中重建,这需要你 20 秒。