问题描述
我有一个可以使用 Visual Studio 正确构建的 ASP.NET Core 项目,但它不能在 MSBuild 下构建.
I have an ASP.NET Core project that builds properly with Visual Studio, but it doesn't build under MSBuild.
它没有找到所有的通用库(系统等).
It doesn't find all the common libraries (system, etc.).
我正在使用 TeamCity,构建过程的一部分是 nuget restore
.
I'm using TeamCity and part of the build process is a nuget restore
.
我尝试执行与 TeamCity 相同的步骤,但使用 MSBuild 手动执行,但失败了,找不到库.
I tried to do the same steps as TeamCity, but manually with MSBuild, and it failed, not finding the libraries.
我添加了一个 dotnet restore
步骤,然后它起作用了.
I added a dotnet restore
step and then it worked.
那么,nuget restore
和 dotnet restore
有什么区别?
So, what is the difference between a nuget restore
and a dotnet restore
?
推荐答案
nuget restore
和 dotnet restore
大致相同:它们执行 NuGet 恢复操作.
Both nuget restore
and dotnet restore
are roughly the same: They perform a NuGet restore operation.
唯一的区别:dotnet restore
是调用 dotnet msbuild/t:Restore
的便捷包装器,它调用 MSBuild 集成的恢复.这仅适用于包含 NuGet 的 MSBuild 发行版,例如 Visual Studio 2017(完整的 Visual Studio,构建工具)或 Mono 5.2+ (=> msbuild/t:Restore
) 和 .NET Core提供此便捷命令的 SDK.
The only difference: dotnet restore
is a convenience wrapper to invoke dotnet msbuild /t:Restore
which invokes an MSBuild-integrated restore. This only works on MSBuild distributions that include NuGet, such as Visual Studio 2017 (full Visual Studio, build tools) or Mono 5.2+ (=> msbuild /t:Restore
) and the .NET Core SDK which provides this convenience command.
目前,有两种方法可以在项目中使用 NuGet 包(实际上是三种,但我们暂时忽略 UWP 上的 project.json
):
At the moment, there are two ways of how NuGet packages can be used in projects (three actually, but let's ignore project.json
on UWP for the moment):
packages.config
:引用 NuGet 包的经典"方式.这假定 NuGet 是一个单独的工具,并且 MSBuild 对 NuGet 一无所知.NuGet 客户端(例如nuget.exe
或 Visual Studio 集成工具)会看到packages.config
文件并在还原时将引用的包下载到本地文件夹中.包安装会修改项目以引用此本地文件夹之外的资产.因此,packages.config
项目的恢复只会下载文件.PackageReference
:项目包含引用 NuGet 包的 MSBuild 项.与packages.config
不同,仅列出了直接依赖项,并且项目文件不直接引用包中的任何资产(DLL 文件、内容文件).在还原时,NuGet 通过评估直接和传递依赖关系来计算依赖关系图,确保所有包都下载到用户的全局包缓存中(不是本地解决方案,因此只下载一次)并将资产文件写入obj
文件夹,其中包含项目使用的所有包和资产的列表,以及其他 MSBuild 目标(如果任何包包含需要添加到项目的构建逻辑).因此,如果包尚未在全局缓存中,NuGet 还原可能会下载包并创建此资产文件.除了包引用,项目还可以引用 CLI 工具,这些工具是 NuGet 包,其中包含可用于项目目录中的dotnet
的附加命令.
packages.config
: The "classic" way of referencing NuGet packages. This assumes NuGet is a separate tool and MSBuild doesn't know anything about NuGet. A NuGet client such asnuget.exe
or Visual Studio-integrated tooling sees thepackages.config
file and downloads the referenced packages into a local folder on restore. A package install modifies the project to reference assets out of this local folder. So a restore for apackages.config
project only downloads the files.PackageReference
: The project contains MSBuild items that reference a NuGet package. Unlikepackages.config
, only the direct dependencies are listed and the project file does not directly reference any assets (DLL files, content files) out of packages. On restore, NuGet figures out the dependency graph by evaluating the direct and transitive dependencies, makes sure all packages are downloaded into the user's global package cache (not solution-local so it is only downloaded once) and write an assets file into theobj
folder that contains a list of all packages and assets that the project uses, as well as additional MSBuild targets if any package contains build logic that needs to be added to a project. So a NuGet restore may download packages if they are not already in the global cache and create this assets file. In addition to package references, the project can also reference CLI tools, which are NuGet packages containing additional commands that will be available for thedotnet
in the project directory.
msbuild-integrated restore 仅适用于 PackageReference
类型的项目(默认为 .NET Standard、.NET Core,但它适用于任何 .NET 项目),不适用于 packages.config
项目.如果您使用新版本的 nuget.exe
(例如 4.3.0),它可以恢复两种项目类型.
The msbuild-integrated restore only works for PackageReference
type projects (.NET Standard, .NET Core by default, but it is opt-in for any .NET project) and not for packages.config
projects. If you use a new version of nuget.exe
(e.g. 4.3.0), it is able to restore both project types.
您关于缺少类型的错误更有趣:参考程序集"(作为输入传递给编译器的库)未安装在系统上,而是通过 NuGet 包安装.因此,只要全局包缓存中缺少 NuGet 包,或者还原操作未生成 obj/project.assets.json
文件,System.Object<等基本类型/code>编译器将无法使用.
Your error about missing types is a bit more interesting: The "reference assemblies" (libraries that are passed as input to the compiler) are not installed on the system but come via NuGet packages. So as long as the NuGet packages are missing from the global package cache or the obj/project.assets.json
file has not been generated by a restore operation, fundamental types like System.Object
will not be available to the compiler.
这篇关于使用 TeamCity 的“dotnet restore"与“nuget restore"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!