.NET Core 應用程式部署.NET Core application deployment

您可以建立三種類型的 .NET Core 應用程式部署︰You can create three types of deployments for .NET Core applications:

  • 與 Framework 相依的部署。Framework-dependent deployment. 正如其名,與 Framework 相依的部署 (framework-dependent deployment, FDD) 仰賴存在於目標系統上的全系統共用 .NET Core 版本。As the name implies, framework-dependent deployment (FDD) relies on the presence of a shared system-wide version of .NET Core on the target system. 因為 .NET Core 已存在,所以應用程式也可以在 .NET Core 安裝之間攜帶。Because .NET Core is already present, your app is also portable between installations of .NET Core. 您的應用程式僅包含其自有程式碼和 .NET Core 程式庫以外的所有協力廠商相依性。Your app contains only its own code and any third-party dependencies that are outside of the .NET Core libraries. FDD 包含的 .dll 檔案,可以使用 dotnet 公用程式從命令列啟動。FDDs contain .dll files that can be launched by using the dotnet utility from the command line. 例如,dotnet app.dll 執行名為 app 的應用程式。For example, dotnet app.dll runs an application named app.

  • 自封式部署。Self-contained deployment. 不同於 FDD,自封式部署 (SCD) 不仰賴任何存在於目標系統上的共用元件。Unlike FDD, a self-contained deployment (SCD) doesn't rely on the presence of shared components on the target system. 包括 .NET Core 程式庫和 .NET Core 執行階段的所有元件,都隨附於應用程式,並與其他 .NET Core 應用程式隔離。All components, including both the .NET Core libraries and the .NET Core runtime, are included with the application and are isolated from other .NET Core applications. SCD 包含可執行檔 (例如,Windows 平台上 app 應用程式的 app.exe),這是重新命名的特定平台 .NET Core 主應用程式版本,以及實際的應用程式 .dll 檔案 (例如 app.dll)。SCDs include an executable (such as app.exe on Windows platforms for an application named app), which is a renamed version of the platform-specific .NET Core host, and a .dll file (such as app.dll), which is the actual application.

  • 架構相依可執行檔。Framework-dependent executables. 產生可在目標平台上執行的可執行檔。Produces an executable that runs on a target platform. 架構相依可執行檔 (FDE) 與 FDDs 類似,它是平台特定且不是自封式。Similar to FDDs, framework-dependent executables (FDE) are platform-specific and aren't self-contained. 這些部署仍相依於共用系統面 .NET Core 版本。These deployments still rely on the presence of a shared system-wide version of .NET Core to run. 與 SCD 不一樣,您的應用程式只包含您的程式碼與 .NET Core 程式庫外的任何第三方相依性。Unlike an SCD, your app only contains your code and any third-party dependencies that are outside of the .NET Core libraries. FDE 會產生在目標平台上執行的可執行檔。FDEs produce an executable that runs on the target platform.

與 Framework 相依的部署 (FDD)Framework-dependent deployments (FDD)

針對 FDD,您只要部署您的應用程式與協力廠商相依性。For an FDD, you deploy only your app and third-party dependencies. 您的應用程式將會使用存在於目標系統上的 .NET Core 版本。Your app will use the version of .NET Core that's present on the target system. 這是以 .NET Core 為目標是 .NET Core 與 ASP.NET Core 應用程式的預設部署模型。This is the default deployment model for .NET Core and ASP.NET Core apps that target .NET Core.

為何建立與 Framework 相依的部署?Why create a framework-dependent deployment?

部署 FDD 有許多優點︰Deploying an FDD has a number of advantages:

  • 您不必事先定義 .NET Core 應用程式執行所在的目標作業系統。You don't have to define the target operating systems that your .NET Core app will run on in advance. 由於不論作業系統為何,.NET Core 對可執行檔和程式庫都使用通用的 PE 檔案格式,所以 .NET Core 可以執行您的應用程式,不理會基礎作業系統。Because .NET Core uses a common PE file format for executables and libraries regardless of operating system, .NET Core can execute your app regardless of the underlying operating system. 如需 PE 檔格式的詳細資訊,請參閱 .NET Assembly File Format (.NET 組件檔案格式)。For more information on the PE file format, see .NET Assembly File Format.

  • 部署套件的大小很小。The size of your deployment package is small. 您只需要部署您的應用程式及其相依性,不用部署 .NET Core。You only deploy your app and its dependencies, not .NET Core itself.

  • 除非被覆寫,否則 FDD 將會使用目標系統上安裝的最新維護執行階段。Unless overridden, FDDs will use the latest serviced runtime installed on the target system. 這可讓您的應用程式使用最新的已修補 .NET Core 執行階段版本。This allows your application to use the latest patched version of the .NET Core runtime.

  • 多個應用程式使用相同的 .NET Core 安裝,這樣可減少主機系統的磁碟空間和記憶體使用量。Multiple apps use the same .NET Core installation, which reduces both disk space and memory usage on host systems.

另外還有幾個缺點︰There are also a few disadvantages:

  • 只有主機系統安裝您的應用程式以其為目標的 .NET Core 版本或更新版本,您的應用程式才能執行。Your app can run only if the version of .NET Core your app targets, or a later version, is already installed on the host system.

  • .NET Core 執行階段和程式庫在未來的版本中可能有所變更,但不會通知您。It's possible for the .NET Core runtime and libraries to change without your knowledge in future releases. 只有極其罕見的情況,才可能變更應用程式的行為。In rare cases, this may change the behavior of your app.

自封式部署 (SCD)Self-contained deployments (SCD)

針對自封式部署,您不僅要部署自己的應用程式和所有協力廠商相依性,還要部署建置應用程式所用的 .NET Core 版本。For a self-contained deployment, you deploy your app and any required third-party dependencies along with the version of .NET Core that you used to build the app. 不過,建立 SCD 不包含各種平台的 .NET Core 原生相依性本身 (例如,macOS 上的 OpenSSL),所以在執行應用程式前要先行安裝。Creating an SCD doesn't include the native dependencies of .NET Core on various platforms, so these must be present before the app runs. 如需執行階段之版本繫結的詳細資訊,請參閱 .NET Core 中的版本繫結上的文章。For more information on version binding at runtime, see the article on version binding in .NET Core.

從 NET Core 2.1 SDK (2.1.300 版) 開始,.NET Core 就支援修補版本向前復原Starting with NET Core 2.1 SDK (version 2.1.300), .NET Core supports patch version roll forward. 當您建立自封式部署時,.NET Core 工具會自動包括您的應用程式以其為目標的最新 .NET Core 維護執行階段版本When you create a self-contained deployment, .NET Core tools automatically include the latest serviced runtime of the .NET Core version that your application targets. (最新的服務執行時間包含安全性修補程式和其他錯誤修正)。服務執行時間不一定要存在於您的組建系統上;它會自動從 NuGet.org 下載。如需詳細資訊,包括如何退出宣告修補程式版本向前復原的指示,請參閱獨立部署執行時間向前復原。(The latest serviced runtime includes security patches and other bug fixes.) The serviced runtime does not have to be present on your build system; it is downloaded automatically from NuGet.org. For more information, including instructions on how to opt out of patch version roll forward, see Self-contained deployment runtime roll forward.

FDD 和 SCD 使用不同的主機可執行檔,因此您可以使用自己的發行者簽章,為 SCD 簽署主機可執行檔。FDD and SCD deployments use separate host executables, so you can sign a host executable for an SCD with your publisher signature.

為什麼要部署自封式部署?Why deploy a self-contained deployment?

部署自封式部署有兩大優點︰Deploying a Self-contained deployment has two major advantages:

  • 您可以獨家控制隨應用程式部署的 .NET Core 版本。You have sole control of the version of .NET Core that is deployed with your app. 只有您可以提供 .NET Core 服務。.NET Core can be serviced only by you.

  • 您可以保證目標系統能執行您的 .NET Core 應用程式,因為您提供的是它可以執行的 .NET Core 版本。You can be assured that the target system can run your .NET Core app, since you're providing the version of .NET Core that it will run on.

它也有一些缺點︰It also has a number of disadvantages:

  • 因為 .NET Core 包含在您的部署套件中,所以您必須事先選取部署套件建置所在的目標平台。Because .NET Core is included in your deployment package, you must select the target platforms for which you build deployment packages in advance.

  • 您的部署套件的大小相當大,因為您必須包含 .NET Core 以及應用程式及其協力廠商相依性。The size of your deployment package is relatively large, since you have to include .NET Core as well as your app and its third-party dependencies.

    從 .NET Core 2.0 開始,您就可以使用 .NET Core 全球化不變模式來減少 Linux 系統上的部署大小大約 28 MB。Starting with .NET Core 2.0, you can reduce the size of your deployment on Linux systems by approximately 28 MB by using .NET Core globalization invariant mode. 一般而言,Linux 上的 .NET Core 依賴 ICU 程式庫來提供全球化支援。Ordinarily, .NET Core on Linux relies on the ICU libraries for globalization support. 在不區分模式中,您的部署不包含程式庫,而且所有文化特性的行為不因文化特性而異In invariant mode, the libraries are not included with your deployment, and all cultures behave like the invariant culture.

  • 將多個自封式 .NET Core 應用程式部署到系統,會消耗大量的磁碟空間,因為每個應用程式都會重複 .NET Core 檔案。Deploying numerous self-contained .NET Core apps to a system can consume significant amounts of disk space, since each app duplicates .NET Core files.

架構相依可執行檔 (FDE)Framework-dependent executables (FDE)

從 .NET Core 2.2 開始,您就可以將您的應用程式部署為 FDE,以及任何必要的第三方相依性。Starting with .NET Core 2.2, you can deploy your app as an FDE, along with any required third-party dependencies. 您的應用程式將會使用安裝在目標系統上的 .NET Core 版本。Your app will use the version of .NET Core that's installed on the target system.

為何部署架構相依可執行檔?Why deploy a framework-dependent executable?

部署 FDE有許多優點︰Deploying an FDE has a number of advantages:

  • 部署套件的大小很小。The size of your deployment package is small. 您只需要部署您的應用程式及其相依性,不用部署 .NET Core。You only deploy your app and its dependencies, not .NET Core itself.

  • 多個應用程式使用相同的 .NET Core 安裝,這樣可減少主機系統的磁碟空間和記憶體使用量。Multiple apps use the same .NET Core installation, which reduces both disk space and memory usage on host systems.

  • 您的應用程式可以透過呼叫已發行的可執行檔來執行,而不需要直接叫用 dotnet 公用程式。Your app can be run by calling the published executable without invoking the dotnet utility directly.

另外還有幾個缺點︰There are also a few disadvantages:

  • 只有主機系統安裝您的應用程式以其為目標的 .NET Core 版本或更新版本,您的應用程式才能執行。Your app can run only if the version of .NET Core your app targets, or a later version, is already installed on the host system.

  • .NET Core 執行階段和程式庫在未來的版本中可能有所變更,但不會通知您。It's possible for the .NET Core runtime and libraries to change without your knowledge in future releases. 只有極其罕見的情況,才可能變更應用程式的行為。In rare cases, this may change the behavior of your app.

  • 您必須為每個目標平台發行您的應用程式。You must publish your app for each target platform.

逐步說明範例Step-by-step examples

如需使用 CLI 工具部署 .NET Core 應用程式的逐步說明範例,請參閱使用 CLI 工具部署 .NET Core 應用程式For step-by-step examples of deploying .NET Core apps with CLI tools, see Deploying .NET Core Apps with CLI Tools. 如需使用 Visual Studio 部署 .NET Core 應用程式的逐步說明範例,請參閱使用 Visual Studio 部署 .NET Core 應用程式For step-by-step examples of deploying .NET Core apps with Visual Studio, see Deploying .NET Core Apps with Visual Studio.

請參閱See also