Azure DevOps,在使用 website.sln 文件时恢复 "Visual Studio Build" 的 NuGet 包

Azure DevOps, restore NuGet packages for "Visual Studio Build" when using a website.sln file

我似乎无法为我的 DevOps 构建管道找到正确的设置以使其能够恢复 NuGet 包以便构建过程找到它们。

我克服的第一个问题是默认设置将还原指向我的 .sln 文件,这导致它打印出来:

Nothing to do. None of the projects in this solution specify any packages for NuGet to restore.

这是一个 Visual Studio 2017 网站 .sln 文件,不包含有关 NuGet 的信息,也没有其他答案建议的 .csproj。

我通过将其指向 packages.config 文件来解决这个问题。现在恢复过程确实找到了需要的包,但它似乎并没有把它们放在 "Visual Studio Build" 构建过程可以找到它们的地方。

Error CS0246: The type or namespace name 'Nest' could not be found (are you missing a using directive or an assembly reference?)

我查看了构建过程部分,但找不到任何有用的信息,因为唯一的参考标记为:

This option is deprecated. To restore NuGet packages, add a NuGet Tool Installer

这是我已经在尝试做的事情。我曾尝试将 NuGet 还原的目标目录移动到 bin 和其他地方,但没有任何区别。

我是否缺少在恢复文件后移动文件的步骤?或者我需要在构建时传递一个参数来告诉它包文件在哪里?

Azure DevOps, Restore NuGet packages for “Visual Studio Build” when using a website.sln?

您应该在构建管道期间使用 NuGet 安装程序任务。

选项恢复 NuGet 包 已弃用:

This option is deprecated. To restore NuGet packages, add a NuGet Installer step before the build

因此,要解决此问题,请尝试使用 NuGet 安装程序 任务,转到“包”选项卡,然后添加 NuGet 恢复任务。

您可以指定packages.config.sln文件来恢复包。指定packages.config时,还需要提供Destination目录,默认值为/packages:

在针对不同的管道查看此内容后,我找到了答案。简而言之,它与 packages.config 和 sln 文件位置以及用于恢复和搜索包的相对路径有关。

长答案。

在生成解决方案日志中,在给出丢失文件的错误后,它列出了它正在检查的所有位置。

For SearchPath "{CandidateAssemblyFiles}".
For SearchPath "{HintPathFromItem}".
For SearchPath "{TargetFrameworkDirectory}".
For SearchPath "bin\Release\".
For SearchPath "{RawFileName}".

HintPathFromItem 最有趣,因为它提到了包文件夹。

For SearchPath "{HintPathFromItem}".
Considered "packages\log4net.2.0.0\lib\net40-full\log4net.dll", but it didn't exist.

考虑到这一点,我检查了 NuGet 还原将包文件夹放置的位置,发现它将它添加到与网站根目录相同的级别。在我的例子中,将目标目录更改为

packages

而不是

/packages

../packages

将其上移一级到网站根目录中构建搜索的位置。

我遇到了与您的第一个问题相同的问题(传递 .sln 文件的 nuget 恢复未恢复解决方案中某个项目的 packages.config 中指定的 nuget 包)。我不想修改 nuget restore 命令直接指向 packages.config(您采用的解决方案)。

我找到的解决方案是将该项目从 packages.config 迁移到新的 Package Reference format。这样做,nuget 恢复 mysolution.sln 工作正常。