獨立式部署執行階段向前復原Self-contained deployment runtime roll forward

.NET Core 獨立式應用程式部署包含 .NET Core l 程式庫和 .NET Core 執行階段。.NET Core self-contained application deployments include both the .NET Core libraries and the .NET Core runtime. 從 NET Core 2.1 SDK (版本 2.1.300) 開始,獨立式應用程式部署會發佈電腦上的最高修補程式執行階段Starting in .NET Core 2.1 SDK (version 2.1.300), a self-contained application deployment publishes the highest patch runtime on your machine. 根據預設,用於獨立式部署的 dotnet publish 會選取發佈電腦上已安裝為 SDK 一部分的最新版本。By default, dotnet publish for a self-contained deployment selects the latest version installed as part of the SDK on the publishing machine. 這可讓您部署的應用程式使用 publish 期間所提供的安全性修正 (和其他修正) 來執行。This enables your deployed application to run with security fixes (and other fixes) available during publish. 應用程式必須重新發佈,才能取得新的修補程式。The application must be re-published to obtain a new patch. 藉由在 dotnet publish 命令上指定 -r <RID>,或者在專案檔 (csproj / vbproj) 中或命令列上指定執行階段識別碼 (RID),即可建立獨立式應用程式。Self-contained applications are created by specifying -r <RID> on the dotnet publish command or by specifying the runtime identifier (RID) in the project file (csproj / vbproj) or on the command line.

修補程式版本向前復原概觀Patch version roll forward overview

restorebuildpublish 是可以分別執行的 dotnet 命令。restore, build and publish are dotnet commands that can run separately. 執行階段選擇屬於 restore 作業的一部分,而不是 publishbuildThe runtime choice is part of the restore operation, not publish or build. 如果您呼叫 publish,將會選擇最新的修補程式版本。If you call publish, the latest patch version will be chosen. 如果您呼叫 publish 搭配 --no-restore 引數,則可能無法取得所需的修補程式版本,因為之前的 restore 可能尚未使用新的獨立式應用程式發佈原則來執行。If you call publish with the --no-restore argument, then you may not get the desired patch version because a prior restore may not have been executed with the new self-contained application publishing policy. 在此情況下,會產生文字類似於下列內容的建置錯誤:In this case, a build error is generated with text similar to the following:

「此專案已使用 Microsoft.NETCore.App 2.0.0 版進行還原,但以目前的設定,將改用 2.0.6 版。"The project was restored using Microsoft.NETCore.App version 2.0.0, but with current settings, version 2.0.6 would be used instead. 若要解決此問題,請確定會針對還原和後續作業 (例如建置或發佈) 使用相同的設定。To resolve this issue, make sure the same settings are used for restore and for subsequent operations such as build or publish. 一般來說,如果在建置或發佈期間 (而不是在還原期間) 設定了 RuntimeIdentifier 屬性,就會發生此問題。」Typically this issue can occur if the RuntimeIdentifier property is set during build or publish but not during restore."

注意

restorebuild 可以當作另一個命令 (如 publish) 的一部分隱含執行。restore and build can be run implicitly as part of another command, like publish. 作為另一個命令的一部分以隱含方式執行時,會提供其他內容給它們,以便產生正確的成品。When run implicitly as part of another command, they are provided with additional context so that the right artifacts are produced. 當您搭配執行階段進行 publish (例如,dotnet publish -r linux-x64),隱含的 restore 會還原 linux-x64 執行階段的套件。When you publish with a runtime (for example, dotnet publish -r linux-x64), the implicit restore restores packages for the linux-x64 runtime. 如果您明確地呼叫 restore,預設不會還原執行階段套件,因為它並沒有該內容。If you call restore explicitly, it does not restore runtime packages by default, because it doesn't have that context.

如何避免在發佈期間進行還原How to avoid restore during publish

執行 restore 作為 publish 作業的一部分有時候可能不適用於您的情況。Running restore as part of the publish operation may be undesirable for your scenario. 若要在建立獨立式應用程式時避免在 publish 期間進行 restore,請執行下列動作:To avoid restore during publish while creating self-contained applications, do the following:

  • RuntimeIdentifiers 屬性設定為要發佈之所有 RID 的分號分隔清單。Set the RuntimeIdentifiers property to a semicolon-separated list of all the RIDs to be published.
  • TargetLatestRuntimePatch 屬性設定為 trueSet the TargetLatestRuntimePatch property to true.

No-restore 引數搭配 dotnet publish 選項No-restore argument with dotnet publish options

如果您想要使用相同的專案檔同時建立獨立式應用程式和與架構相依的應用程式,而且想要使用 --no-restore 引數搭配 dotnet publish,則選擇下列其中一項:If you want to create both self-contained applications and framework-dependent applications with the same project file, and you want to use the --no-restore argument with dotnet publish, then choose one of the following:

  1. 偏好與架構相依的行為。Prefer the framework-dependent behavior. 如果應用程式是與架構相依的應用程式,這是預設行為。If the application is framework-dependent, this is the default behavior. 如果應用程式是獨立式應用程式,且可以使用未修補的 2.1.0 本機執行階段,請在專案檔中將 TargetLatestRuntimePatch 設定為 falseIf the application is self-contained, and can use an unpatched 2.1.0 local runtime, set the TargetLatestRuntimePatch to false in the project file.

  2. 偏好獨立式行為。Prefer the self-contained behavior. 如果應用程式是獨立式應用程式,這是預設行為。If the application is self-contained, this is the default behavior. 如果應用程式是與架構相依的應用程式,且需要安裝最新的修補程式,請在專案檔中將 TargetLatestRuntimePatch 設定為 trueIf the application is framework-dependent, and requires the latest patch installed, set TargetLatestRuntimePatch to true in the project file.

  3. 在專案檔中將 RuntimeFrameworkVersion 設定為特定的修補程式版本,藉以取得執行階段架構版本的明確控制權。Take explicit control of the runtime framework version by setting RuntimeFrameworkVersion to the specific patch version in the project file.