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.).
I'm using TeamCity and part of the build process is a nuget restore
.
I tried to do the same steps as TeamCity, but manually with MSBuild, and it failed, not finding the libraries.
I added a dotnet restore
step and then it worked.
So, what is the difference between a nuget restore
and a dotnet restore
?
The dotnet restore command uses NuGet to restore dependencies as well as project-specific tools that are specified in the project file. In most cases, you don't need to explicitly use the dotnet restore command, since a NuGet restore is run implicitly if necessary when you run the following commands: dotnet new.
To promote a cleaner development environment and to reduce repository size, NuGet Package Restore installs all of a project's dependencies listed in either the project file or packages. config . The . NET Core 2.0+ dotnet build and dotnet run commands do an automatic package restore.
The dotnet restore command is still useful in certain scenarios where explicitly restoring makes sense, such as continuous integration builds in Azure DevOps Services or in build systems that need to explicitly control when the restore occurs.
The dotnet tool restore command finds the tool manifest file that is in scope for the current directory and installs the tools that are listed in it. For information about manifest files, see Install a local tool and Invoke a local tool.
Both nuget restore
and dotnet restore
are roughly the same: They perform a NuGet restore operation.
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.
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
: 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 as nuget.exe
or Visual Studio-integrated tooling sees the packages.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 a packages.config
project only downloads the files.PackageReference
: The project contains MSBuild items that reference a NuGet package. Unlike packages.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 the obj
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 the dotnet
in the project directory.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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With