.NET Core 應用程式發行總覽.NET Core application publishing overview

您使用 .NET Core 建立的應用程式可以在兩種不同的模式下發行,而此模式會影響使用者執行您應用程式的方式。Applications you create with .NET Core can be published in two different modes, and the mode affects how a user runs your app.

將您的應用程式發佈為 獨立 應用程式時,會產生包含 .net Core 執行時間和程式庫的應用程式,以及您的應用程式及其相依性。Publishing your app as self-contained produces an application that includes the .NET Core runtime and libraries, and your application and its dependencies. 應用程式的使用者可以在未安裝 .NET Core 執行時間的電腦上執行它。Users of the application can run it on a machine that doesn't have the .NET Core runtime installed.

將您的應用程式發佈為與 framework 相依 的專案,會產生只包含應用程式本身及其相依性的應用程式。Publishing your app as framework-dependent produces an application that includes only your application itself and its dependencies. 應用程式的使用者必須分別安裝 .NET Core 執行時間。Users of the application have to separately install the .NET Core runtime.

這兩種發行模式預設都會產生平臺特定的可執行檔。Both publishing modes produce a platform-specific executable by default. 您可以建立不含可執行檔的 Framework 相依應用程式,而這些應用程式會跨平臺。Framework-dependent applications can be created without an executable, and these applications are cross-platform.

當產生可執行檔時,您可以使用執行時間識別碼 (RID) 來指定目標平臺。When an executable is produced, you can specify the target platform with a runtime identifier (RID). 如需有關 Rid 的詳細資訊,請參閱 .Net CORE RID 目錄For more information about RIDs, see .NET Core RID Catalog.

下表概述每個 SDK 版本用來將應用程式發佈為架構相依或獨立的命令:The following table outlines the commands used to publish an app as framework-dependent or self-contained, per SDK version:

類型Type SDK 2.1SDK 2.1 SDK 3。xSDK 3.x CommandCommand
適用于目前平臺的framework 相依可執行檔framework-dependent executable for the current platform. ✔️✔️ dotnet publish
特定平臺的framework 相依可執行檔framework-dependent executable for a specific platform. ✔️✔️ dotnet publish -r <RID> --self-contained false
framework 相依的跨平臺二進位檔。framework-dependent cross-platform binary. ✔️✔️ ✔️✔️ dotnet publish
獨立可執行檔self-contained executable. ✔️✔️ ✔️✔️ dotnet publish -r <RID>

如需詳細資訊,請參閱 .Net Core dotnet publish 命令For more information, see .NET Core dotnet publish command.

產生可執行檔Produce an executable

可執行檔不是跨平臺。Executables aren't cross-platform. 它們是專屬於作業系統和 CPU 架構所特有。They're specific to an operating system and CPU architecture. 當您發行應用程式並建立可執行檔時,您可以將應用程式發佈為 獨立 或與 framework 相依的應用程式。When publishing your app and creating an executable, you can publish the app as self-contained or framework-dependent. 將應用程式發佈為獨立應用程式時,會包含 .NET Core 執行時間與應用程式,而應用程式的使用者在執行應用程式之前,不需要擔心安裝 .NET Core。Publishing an app as self-contained includes the .NET Core runtime with the app, and users of the app don't have to worry about installing .NET Core before running the app. 發佈為與 framework 相依的應用程式不包含 .NET Core 執行時間和程式庫;只會包含應用程式和協力廠商相依性。Apps published as framework-dependent don't include the .NET Core runtime and libraries; only the app and 3rd-party dependencies are included.

下列命令會產生可執行檔:The following commands produce an executable:

類型Type SDK 2.1SDK 2.1 SDK 3。xSDK 3.x CommandCommand
適用于目前平臺的framework 相依可執行檔framework-dependent executable for the current platform. ✔️✔️ dotnet publish
特定平臺的framework 相依可執行檔framework-dependent executable for a specific platform. ✔️✔️ dotnet publish -r <RID> --self-contained false
獨立可執行檔self-contained executable. ✔️✔️ ✔️✔️ dotnet publish -r <RID>

產生跨平臺二進位檔Produce a cross-platform binary

當您將應用程式發佈為與 framework 相依的應用程式時,會建立跨平臺二進位檔(以 dll 檔案的形式)。Cross-platform binaries are created when you publish your app as framework-dependent, in the form of a dll file. Dll檔案會以您的專案命名。The dll file is named after your project. 例如,如果您有一個名為 word_reader的應用程式,則會建立名為 word_reader.dll 的檔案。For example, if you have an app named word_reader, a file named word_reader.dll is created. 以這種方式發佈的應用程式會使用 dotnet <filename.dll> 命令執行,而且可以在任何平臺上執行。Apps published in this way are run with the dotnet <filename.dll> command and can be run on any platform.

只要已安裝目標 .NET Core 執行時間,就可以在任何作業系統上執行跨平臺二進位檔。Cross-platform binaries can be run on any operating system as long as the targeted .NET Core runtime is already installed. 如果未安裝目標 .NET Core 執行時間,應用程式可以使用較新的執行時間執行(如果應用程式設定為向前復原)。If the targeted .NET Core runtime isn't installed, the app may run using a newer runtime if the app is configured to roll-forward. 如需詳細資訊,請參閱 與 framework 相依的應用程式向前復原。For more information, see framework-dependent apps roll forward.

下列命令會產生跨平臺二進位檔:The following command produces a cross-platform binary:

類型Type SDK 2.1SDK 2.1 SDK 3。xSDK 3.x CommandCommand
framework 相依的跨平臺二進位檔。framework-dependent cross-platform binary. ✔️✔️ ✔️✔️ dotnet publish

發佈 framework 相依Publish framework-dependent

發佈為與 framework 相依的應用程式是跨平臺,不包含 .NET Core 執行時間。Apps published as framework-dependent are cross-platform and don't include the .NET Core runtime. 您應用程式的使用者必須安裝 .NET Core 執行時間。The user of your app is required to install the .NET Core runtime.

將應用程式發佈為與 framework 相依的應用程式,會產生 跨平臺二進位 檔做為 dll 檔案,以及以您目前平臺為目標的 平臺特定可執行 檔。Publishing an app as framework-dependent produces a cross-platform binary as a dll file, and a platform-specific executable that targets your current platform. Dll是跨平臺,而可執行檔則不是。The dll is cross-platform while the executable isn't. 例如,如果您發行名為word_reader和目標 Windows 的應用程式,則會隨著word_reader.dll建立word_reader.exe可執行檔。For example, if you publish an app named word_reader and target Windows, a word_reader.exe executable is created along with word_reader.dll. 以 Linux 或 macOS 為目標時,會建立 word_reader 可執行檔以及 word_reader.dllWhen targeting Linux or macOS, a word_reader executable is created along with word_reader.dll. 如需有關 Rid 的詳細資訊,請參閱 .Net CORE RID 目錄For more information about RIDs, see .NET Core RID Catalog.

重要

當您發佈與應用程式架構相依的應用程式時,.NET Core SDK 2.1 不會產生平臺特定的可執行檔。.NET Core SDK 2.1 doesn't produce platform-specific executables when you publish an app framework-dependent.

您可以使用命令來執行應用程式的跨平臺二進位檔 dotnet <filename.dll> ,並可在任何平臺上執行。The cross-platform binary of your app can be run with the dotnet <filename.dll> command, and can be run on any platform. 如果應用程式使用的 NuGet 套件具有平臺特定的執行,則所有平臺的相依性都會連同應用程式一起複製到 [發佈] 資料夾。If the app uses a NuGet package that has platform-specific implementations, all platforms' dependencies are copied to the publish folder along with the app.

您可以藉由將參數傳遞至命令,為特定平臺建立可執行檔 -r <RID> --self-contained false dotnet publishYou can create an executable for a specific platform by passing the -r <RID> --self-contained false parameters to the dotnet publish command. 如果 -r 省略此參數,就會為您目前的平臺建立可執行檔。When the -r parameter is omitted, an executable is created for your current platform. 任何具有目標平臺之平臺特定相依性的 NuGet 套件都會複製到 [發佈] 資料夾。Any NuGet packages that have platform-specific dependencies for the targeted platform are copied to the publish folder.

優點Advantages

  • 小規模部署Small deployment
    只會發佈您的應用程式及其相依性。Only your app and its dependencies are distributed. 使用者會安裝 .NET Core 執行時間和程式庫,而且所有應用程式會共用執行時間。The .NET Core runtime and libraries are installed by the user and all apps share the runtime.

  • 跨平臺Cross-platform
    您的應用程式和任何。以網路為基礎的程式庫會在其他作業系統上執行。Your app and any .NET-based library runs on other operating systems. 您不需要為您的應用程式定義目標平臺。You don't need to define a target platform for your app. 如需 .NET 檔案格式的詳細資訊,請參閱 .Net 元件檔案格式For information about the .NET file format, see .NET Assembly File Format.

  • 使用最新的修補執行時間Uses the latest patched runtime
    應用程式會使用最新的執行時間 (在目標系統上安裝之 .NET Core 的目標主要核心) 內。The app uses the latest runtime (within the targeted major-minor family of .NET Core) installed on the target system. 這表示您的應用程式會自動使用 .NET Core 執行時間的最新修補版本。This means your app automatically uses the latest patched version of the .NET Core runtime. 您可以覆寫這個預設行為。This default behavior can be overridden. 如需詳細資訊,請參閱 與 framework 相依的應用程式向前復原。For more information, see framework-dependent apps roll forward.

缺點Disadvantages

  • 需要預先安裝執行時間Requires pre-installing the runtime
    只有當應用程式的目標 .NET Core 版本已安裝在主機系統上時,您的應用程式才能執行。Your app can run only if the version of .NET Core your app targets is already installed on the host system. 您可以將應用程式的向前復原行為設定為需要特定版本的 .NET Core,或允許較新版本的 .NET Core。You can configure roll-forward behavior for the app to either require a specific version of .NET Core or allow a newer version of .NET Core. 如需詳細資訊,請參閱 與 framework 相依的應用程式向前復原。For more information, see framework-dependent apps roll forward.

  • .NET Core 可能會變更.NET Core may change
    您可以在執行應用程式的電腦上更新 .NET Core 執行時間和程式庫。It's possible for the .NET Core runtime and libraries to be updated on the machine where the app is run. 在罕見的情況下,如果您使用的是大部分應用程式所使用的 .NET Core 程式庫,這可能會變更您的應用程式行為。In rare cases, this may change the behavior of your app if you use the .NET Core libraries, which most apps do. 您可以設定應用程式如何使用較新版本的 .NET Core。You can configure how your app uses newer versions of .NET Core. 如需詳細資訊,請參閱 與 framework 相依的應用程式向前復原。For more information, see framework-dependent apps roll forward.

下列缺點僅適用于 .NET Core 2.1 SDK。The following disadvantage only applies to .NET Core 2.1 SDK.

  • 使用 dotnet 命令來啟動應用程式Use the dotnet command to start the app
    使用者必須執行 dotnet <filename.dll> 命令以啟動您的應用程式。Users must run the dotnet <filename.dll> command to start your app. .NET Core 2.1 SDK 不會針對已發佈 framework 相依的應用程式,產生平臺特定的可執行檔。.NET Core 2.1 SDK doesn't produce platform-specific executables for apps published framework-dependent.

範例Examples

發佈跨平臺架構相依的應用程式。Publish an app cross-platform framework-dependent. 以目前平臺為目標的可執行檔會與 dll 檔案一起建立。An executable that targets your current platform is created along with the dll file.

dotnet publish

發佈跨平臺架構相依的應用程式。Publish an app cross-platform framework-dependent. Linux 64 位可執行檔會與 dll 檔案一起建立。A Linux 64-bit executable is created along with the dll file. 此命令不適用於 .NET Core SDK 2.1。This command doesn't work with .NET Core SDK 2.1.

dotnet publish -r linux-x64 --self-contained false

獨立發行Publish self-contained

將您的應用程式發佈為獨立應用程式,會產生平臺特定的可執行檔。Publishing your app as self-contained produces a platform-specific executable. 輸出發行資料夾包含應用程式的所有元件,包括 .NET Core 程式庫和目標執行時間。The output publishing folder contains all components of the app, including the .NET Core libraries and target runtime. 應用程式會與其他 .NET Core 應用程式隔離,而且不會使用本機安裝的共用執行時間。The app is isolated from other .NET Core apps and doesn't use a locally installed shared runtime. 您應用程式的使用者不需要下載和安裝 .NET Core。The user of your app isn't required to download and install .NET Core.

針對指定的目標平臺產生可執行檔二進位檔。The executable binary is produced for the specified target platform. 例如,如果您有一個名為 word_reader的應用程式,而且您發行了 Windows 的獨立可執行檔,則會建立 word_reader.exe 檔。For example, if you have an app named word_reader, and you publish a self-contained executable for Windows, a word_reader.exe file is created. 針對 Linux 或 macOS 發行,會建立 word_reader 檔案。Publishing for Linux or macOS, a word_reader file is created. 使用命令的參數指定目標平臺和架構 -r <RID> dotnet publishThe target platform and architecture is specified with the -r <RID> parameter for the dotnet publish command. 如需有關 Rid 的詳細資訊,請參閱 .Net CORE RID 目錄For more information about RIDs, see .NET Core RID Catalog.

如果應用程式具有平臺特定的相依性(例如包含平臺特定相依性的 NuGet 套件),則會將這些相依性連同應用程式一起複製到 [發佈] 資料夾。If the app has platform-specific dependencies, such as a NuGet package containing platform-specific dependencies, these are copied to the publish folder along with the app.

優點Advantages

  • 控制 .NET Core 版本Control .NET Core version
    您可以控制您的應用程式所部署的 .NET Core 版本。You control which version of .NET Core is deployed with your app.

  • 平臺特定目標Platform-specific targeting
    因為您必須為每個平臺發佈您的應用程式,所以您知道應用程式將執行的位置。Because you have to publish your app for each platform, you know where your app will run. 如果 .NET Core 引進新平臺,使用者將無法在該平臺上執行您的應用程式,除非您發行以該平臺為目標的版本。If .NET Core introduces a new platform, users can't run your app on that platform until you release a version targeting that platform. 您可以在使用者于新的平臺上執行應用程式之前,先測試應用程式的相容性問題。You can test your app for compatibility problems before your users run your app on the new platform.

缺點Disadvantages

  • 較大型的部署Larger deployments
    因為您的應用程式包含 .NET Core 執行時間和您所有的應用程式相依性,所以需要的下載大小和硬碟空間大於 framework 相依 的版本。Because your app includes the .NET Core runtime and all of your app dependencies, the download size and hard drive space required is greater than a framework-dependent version.

    提示

    您可以使用 .NET Core 全球化非變異模式,將 Linux 系統上的部署大小減少約 28 MB。You can reduce the size of your deployment on Linux systems by approximately 28 MB by using .NET Core globalization invariant mode. 這會強制您的應用程式將所有文化特性視為不因 文化特性而異。This forces your app to treat all cultures like the invariant culture.

    提示

    有一 項預覽修剪功能 可進一步減少部署的大小。There is a preview Trim feature that can further reduce the size of your deployment.

  • 較難更新 .NET Core 版本Harder to update the .NET Core version
    .NET Core 執行時間 (隨您的應用程式散發) 只能透過發行新版的應用程式來進行升級。.NET Core Runtime (distributed with your app) can only be upgraded by releasing a new version of your app. 不過,.NET Core 會在應用程式執行所在的電腦中,視需要更新架構程式庫所需的重大安全性修補程式。However, .NET Core will update critical security patches as needed for the framework library in the machine that your app runs on. 您必須負責進行此安全性修補程式案例的端對端驗證。You are responsible for end to end validation for this security patch scenario.

範例Examples

發行獨立應用程式。Publish an app self-contained. 建立 macOS 64 位可執行檔。A macOS 64-bit executable is created.

dotnet publish -r osx-x64

發行獨立應用程式。Publish an app self-contained. 建立 Windows 64 位可執行檔。A Windows 64-bit executable is created.

dotnet publish -r win-x64

使用 ReadyToRun 映射發佈Publish with ReadyToRun images

使用 ReadyToRun 映射發佈將可改善應用程式的啟動時間,代價是增加應用程式的大小。Publishing with ReadyToRun images will improve the startup time of your application at the cost of increasing the size of your application. 若要使用 ReadyToRun 發佈,請參閱 ReadyToRun 以取得詳細資料。In order to publish with ReadyToRun see ReadyToRun for more details.

優點Advantages

  • 改善的啟動時間Improved startup time
    應用程式將花費較少的時間執行 JIT。The application will spend less time running the JIT.

缺點Disadvantages

  • 較大的大小Larger size
    應用程式將會在磁片上放大。The application will be larger on disk.

範例Examples

發行獨立和 ReadyToRun 的應用程式。Publish an app self-contained and ReadyToRun. 建立 macOS 64 位可執行檔。A macOS 64-bit executable is created.

dotnet publish -c Release -r osx-x64 -p:PublishReadyToRun=true

發行獨立和 ReadyToRun 的應用程式。Publish an app self-contained and ReadyToRun. 建立 Windows 64 位可執行檔。A Windows 64-bit executable is created.

dotnet publish -c Release -r win-x64 -p:PublishReadyToRun=true

另請參閱See also