問題描述
我有一個可以使用 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"的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!