dotnet restoredotnet restore

本文适用于: ✔️ .NET Core 2.1 SDK 及更高版本This article applies to: ✔️ .NET Core 2.1 SDK and later versions

“属性”Name

dotnet restore - 恢复项目的依赖项和工具。dotnet restore - Restores the dependencies and tools of a project.

摘要Synopsis

dotnet restore [<ROOT>] [--configfile <FILE>] [--disable-parallel]
    [-f|--force] [--force-evaluate] [--ignore-failed-sources]
    [--interactive] [--lock-file-path <LOCK_FILE_PATH>] [--locked-mode]
    [--no-cache] [--no-dependencies] [--packages <PACKAGES_DIRECTORY>]
    [-r|--runtime <RUNTIME_IDENTIFIER>] [-s|--source <SOURCE>]
    [--use-lock-file] [-v|--verbosity <LEVEL>]

dotnet restore -h|--help

描述Description

dotnet restore 命令使用 NuGet 还原依赖项以及在 project 文件中指定的特定于项目的工具。The dotnet restore command uses NuGet to restore dependencies as well as project-specific tools that are specified in the project file. 在大多数情况下,不需要显式使用 dotnet restore 命令,因为在运行以下命令时,将会在必要时隐式运行 NuGet 还原: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:

有时,通过这些命令运行隐式 NuGet 还原可能不方便。Sometimes, it might be inconvenient to run the implicit NuGet restore with these commands. 例如,某些自动化系统(如生成系统)需要显式调用 dotnet restore,以控制还原发生的时间,以便可以控制网络使用量。For example, some automated systems, such as build systems, need to call dotnet restore explicitly to control when the restore occurs so that they can control network usage. 为了防止运行隐式 NuGet 还原,可以通过上述任意命令使用 --no-restore 标记禁用隐式还原。To prevent the implicit NuGet restore, you can use the --no-restore flag with any of these commands to disable implicit restore.

指定源Specify feeds

为了还原依赖项,NuGet 需要包所在的源。To restore the dependencies, NuGet needs the feeds where the packages are located. 通常通过“nuGet.config”配置文件提供源。Feeds are usually provided via the nuget.config configuration file. 安装 .NET SDK 时提供一个默认的配置文件。A default configuration file is provided when the .NET SDK is installed. 若要指定其他源,请执行以下任一项操作:To specify additional feeds, do one of the following:

可以使用 -s 选项替代 nuget.config 源。You can override the nuget.config feeds with the -s option.

有关如何使用经过身份验证的源的信息,请参阅使用经过身份验证的源中的包For information about how to use authenticated feeds, see Consuming packages from authenticated feeds.

全局包文件夹Global packages folder

对于依赖项,可以使用 --packages 参数指定还原操作期间放置还原包的位置。For dependencies, you can specify where the restored packages are placed during the restore operation using the --packages argument. 如未指定,将使用默认的 NuGet 包缓存,可在所有操作系统上的用户主目录中的 .nuget/packages 目录找到它。If not specified, the default NuGet package cache is used, which is found in the .nuget/packages directory in the user's home directory on all operating systems. 例如 Linux 上的 /home/user1 或 Windows 上的 C:\Users\user1 。For example, /home/user1 on Linux or C:\Users\user1 on Windows.

特定于项目的工具Project-specific tooling

对于特定于项目的工具,dotnet restore 首先还原打包工具所在的包,然后继续还原 project 文件中指定的工具依赖项。For project-specific tooling, dotnet restore first restores the package in which the tool is packed, and then proceeds to restore the tool's dependencies as specified in its project file.

nuget.config 差异nuget.config differences

dotnet restore 命令的行为会受 Nuget.Config 文件(如果有)中某些设置的影响。The behavior of the dotnet restore command is affected by the settings in the nuget.config file, if present. 例如,在 NuGet.Config 中设置 globalPackagesFolder 会将还原的 NuGet 包置于指定的文件夹中。For example, setting the globalPackagesFolder in nuget.config places the restored NuGet packages in the specified folder. 这是在 dotnet restore 命令中指定 --packages 选项的替代方法。This is an alternative to specifying the --packages option on the dotnet restore command. 有关详细信息,请参阅 nuget.config 参考For more information, see the nuget.config reference.

有三个 dotnet restore 可忽略的特定设置:There are three specific settings that dotnet restore ignores:

  • bindingRedirectsbindingRedirects

    绑定重定向不适用于 <PackageReference> 元素,并且 .NET 仅支持 NuGet 包的 <PackageReference> 元素。Binding redirects don't work with <PackageReference> elements and .NET only supports <PackageReference> elements for NuGet packages.

  • 解决方案solution

    此设置特定于 Visual Studio,不适用于 .NET。This setting is Visual Studio specific and doesn't apply to .NET. .NET 不使用 packages.config 文件,而是使用 NuGet 包的 <PackageReference> 元素。.NET doesn't use a packages.config file and instead uses <PackageReference> elements for NuGet packages.

  • trustedSignerstrustedSigners

    此设置不适用,如 NuGet 尚不支持跨平台验证受信任包所述。This setting isn't applicable as NuGet doesn't yet support cross-platform verification of trusted packages.

自变量Arguments

  • ROOT

    要还原的项目文件的可选路径。Optional path to the project file to restore.

选项Options

  • --configfile <FILE>

    供还原操作使用的 NuGet 配置文件 (nuget.config)。The NuGet configuration file (nuget.config) to use for the restore operation.

  • --disable-parallel

    禁用并行还原多个项目。Disables restoring multiple projects in parallel.

  • --force

    强制解析所有依赖项,即使上次还原已成功,也不例外。Forces all dependencies to be resolved even if the last restore was successful. 指定此标记等同于删除 project.assets.json 文件。Specifying this flag is the same as deleting the project.assets.json file.

  • --force-evaluate

    即使锁定文件已存在,也会强制还原以重新评估所有依赖项。Forces restore to reevaluate all dependencies even if a lock file already exists.

  • -h|--help

    打印出有关命令的简短帮助。Prints out a short help for the command.

  • --ignore-failed-sources

    如果存在符合版本要求的包,则源失败时警告。Only warn about failed sources if there are packages meeting the version requirement.

  • --interactive

    允许命令停止并等待用户输入或操作(例如,完成身份验证)。Allows the command to stop and wait for user input or action (for example to complete authentication). 从 .NET Core 2.1.400 开始。Since .NET Core 2.1.400.

  • --lock-file-path <LOCK_FILE_PATH>

    写入项目锁定文件的输出位置。Output location where project lock file is written. 默认情况下,此位置为 PROJECT_ROOT \packages.lock.json。By default, this is PROJECT_ROOT\packages.lock.json.

  • --locked-mode

    不允许更新项目锁定文件。Don't allow updating project lock file.

  • --no-cache

    指定不缓存 HTTP 请求。Specifies to not cache HTTP requests.

  • --no-dependencies

    当使用项目到项目 (P2P) 引用还原项目时,还原根项目,不还原引用。When restoring a project with project-to-project (P2P) references, restores the root project and not the references.

  • --packages <PACKAGES_DIRECTORY>

    指定还原包的目录。Specifies the directory for restored packages.

  • -r|--runtime <RUNTIME_IDENTIFIER>

    指定程序包还原的运行时。Specifies a runtime for the package restore. 这用于还原 .csproj 文件中的 <RuntimeIdentifiers> 标记中未显式列出的运行时的程序包。This is used to restore packages for runtimes not explicitly listed in the <RuntimeIdentifiers> tag in the .csproj file. 有关运行时标识符 (RID) 的列表,请参阅 RID 目录For a list of Runtime Identifiers (RIDs), see the RID catalog. 通过多次指定此选项提供多个 RID。Provide multiple RIDs by specifying this option multiple times.

  • -s|--source <SOURCE>

    指定要在还原操作期间使用的 NuGet 包源的 URI。Specifies the URI of the NuGet package source to use during the restore operation. 此设置会替代 nuget.config 文件中指定的所有源。This setting overrides all of the sources specified in the nuget.config files. 多次指定此选项可以提供多个源。Multiple sources can be provided by specifying this option multiple times.

  • --use-lock-file

    允许生成项目锁定文件并与还原一起使用。Enables project lock file to be generated and used with restore.

  • -v|--verbosity <LEVEL>

    设置命令的详细级别。Sets the verbosity level of the command. 允许使用的值为 q[uiet]m[inimal]n[ormal]d[etailed]diag[nostic]Allowed values are q[uiet], m[inimal], n[ormal], d[etailed], and diag[nostic]. 默认值是 minimalDefault value is minimal.

示例Examples

  • 还原当前目录中项目的依赖项和工具:Restore dependencies and tools for the project in the current directory:

    dotnet restore
    
  • 还原在给定路径中找到的 app1 项目的依赖项和工具:Restore dependencies and tools for the app1 project found in the given path:

    dotnet restore ./projects/app1/app1.csproj
    
  • 通过将提供的文件路径用作源,在当前目录中还原项目的依赖项和工具:Restore the dependencies and tools for the project in the current directory using the file path provided as the source:

    dotnet restore -s c:\packages\mypackages
    
  • 通过将提供的两个文件路径用作源,在当前目录中还原项目的依赖项和工具:Restore the dependencies and tools for the project in the current directory using the two file paths provided as sources:

    dotnet restore -s c:\packages\mypackages -s c:\packages\myotherpackages
    
  • 还原当前目录中项目的依赖项和工具,并显示详细的输出:Restore dependencies and tools for the project in the current directory showing detailed output:

    dotnet restore --verbosity detailed