使用 CLI 發佈 .NET Core 應用程式Publish .NET Core apps with the CLI

此文章示範如何從命令列發佈 .NET Core 應用程式。This article demonstrates how you can publish your .NET Core application from the command line. .NET Core 提供三種發佈應用程式的方式。.NET Core provides three ways to publish your applications. Framework 相依部署會產生跨平台的 .dll 檔案,以使用本機安裝的 .NET Core 執行階段。Framework-dependent deployment produces a cross-platform .dll file that uses the locally installed .NET Core runtime. Framework 相依可執行檔會產生平台特定的可執行檔,來使用本機安裝的 .NET Core 執行階段。Framework-dependent executable produces a platform-specific executable that uses the locally installed .NET Core runtime. 獨立式可執行檔則會產生平台特定的可執行檔,並包含 .NET Core 執行階段的本機複本。Self-contained executable produces a platform-specific executable and includes a local copy of the .NET Core runtime.

如需這些發佈模式的概觀,請參閱 .NET Core 應用程式部署For an overview of these publishing modes, see .NET Core Application Deployment.

需要一些使用 CLI 的快速說明嗎?Looking for some quick help on using the CLI? 下表顯示一些如何發佈應用程式的範例。The following table shows some examples of how to publish your app. 您可以使用 -f <TFM> 參數或藉由編輯專案檔來指定目標 Framework。You can specify the target framework with the -f <TFM> parameter or by editing the project file. 如需詳細資訊,請參閱發佈基本概念For more information, see Publishing basics.

發佈模式Publish Mode SDK 版本SDK Version 命令Command
與 Framework 相依的部署Framework-dependent deployment 2.x2.x dotnet publish -c Release
Framework 相依可執行檔Framework-dependent executable 2.22.2 dotnet publish -c Release -r <RID> --self-contained false
3.03.0 dotnet publish -c Release -r <RID> --self-contained false
3.0*3.0* dotnet publish -c Release
自封式部署Self-contained deployment 2.12.1 dotnet publish -c Release -r <RID> --self-contained true
2.22.2 dotnet publish -c Release -r <RID> --self-contained true
3.03.0 dotnet publish -c Release -r <RID> --self-contained true

* 使用 SDK 3.0 版時,相依於架構的可執行檔是執行基本 dotnet publish 命令時的預設發佈模式。* When using SDK version 3.0, framework-dependent executable is the default publishing mode when running the basic dotnet publish command. 這只適用於以 .NET Core 2.1.NET Core 3.0 為目標的專案。This only applies when the project targets either .NET Core 2.1 or .NET Core 3.0.

發佈基本概念Publishing basics

專案檔的 <TargetFramework> 設定會指定發佈應用程式時的預設目標 Framework。The <TargetFramework> setting of the project file specifies the default target framework when you publish your app. 您可以將目標 Framework 變更為任何有效的目標 Framework Moniker (TFM)You can change the target framework to any valid Target Framework Moniker (TFM). 例如,如果您的專案使用 <TargetFramework>netcoreapp2.2</TargetFramework>,就會建立以 .NET Core 2.2 為目標的二進位檔。For example, if your project uses <TargetFramework>netcoreapp2.2</TargetFramework>, a binary that targets .NET Core 2.2 is created. 此設定中所指定 TFM 是 dotnet publish 命令使用的預設目標。The TFM specified in this setting is the default target used by the dotnet publish command.

如果您想要以多個 Framework 為目標,則可以將 <TargetFrameworks> 設定設為以分號隔開的多個 TFM 值。If you want to target more than one framework, you can set the <TargetFrameworks> setting to more than one TFM value separated by a semicolon. 您可以使用 dotnet publish -f <TFM> 命令來發佈其中一個 Framework。You can publish one of the frameworks with the dotnet publish -f <TFM> command. 例如,如果您的專案具有 <TargetFrameworks>netcoreapp2.1;netcoreapp2.2</TargetFrameworks> 並執行 dotnet publish -f netcoreapp2.1,就會建立以 .NET Core 2.1 為目標的二進位檔。For example, if you have <TargetFrameworks>netcoreapp2.1;netcoreapp2.2</TargetFrameworks> and run dotnet publish -f netcoreapp2.1, a binary that targets .NET Core 2.1 is created.

除非另行設定,否則 dotnet publish 命令的輸出目錄為 ./bin/<BUILD-CONFIGURATION>/<TFM>/publish/Unless otherwise set, the output directory of the dotnet publish command is ./bin/<BUILD-CONFIGURATION>/<TFM>/publish/. 除非使用 -c 參數加以變更,否則預設的組建組態模式為 [偵錯]。The default BUILD-CONFIGURATION mode is Debug unless changed with the -c parameter. 例如,dotnet publish -c Release -f netcoreapp2.1 會發佈至 myfolder/bin/Release/netcoreapp2.1/publish/For example, dotnet publish -c Release -f netcoreapp2.1 publishes to myfolder/bin/Release/netcoreapp2.1/publish/.

如果您使用 .NET Core SDK 3.0,則以 .NET Core 2.1 版、2.2 版或 3.0 版為目標的應用程式其預設發佈模式為 Framework 相依可執行檔。If you use .NET Core SDK 3.0, the default publish mode for apps that target .NET Core versions 2.1, 2.2, or 3.0 is framework-dependent executable.

如果您使用 .NET Core SDK 2.1,則以 .NET Core 2.1 版、2.2 版為目標的應用程式其預設發佈模式為 Framework 相依部署。If you use .NET Core SDK 2.1, the default publish mode for apps that target .NET Core versions 2.1, 2.2 is framework-dependent deployment.

原生相依性Native dependencies

如果您的應用程式具有原生相依性,它可能無法在不同的作業系統上執行。If your app has native dependencies, it may not run on a different operating system. 例如,如果您的應用程式使用原生 Windows API,它就不會在 macOS 或 Linux 上執行。For example, if your app uses the native Windows API, it won't run on macOS or Linux. 您必須提供平台特定程式碼,並為每個平台編譯可執行檔。You would need to provide platform-specific code and compile an executable for each platform.

另外,請考慮如果您參考的程式庫具有原生相依性,您的應用程式可能無法在每個平台上執行。Consider also, if a library you referenced has a native dependency, your app may not run on every platform. 不過,您參考的 NuGet 套件可能包含了平台特定版本,以便為您處理必要的原生相依性。However, it's possible a NuGet package you're referencing has included platform-specific versions to handle the required native dependencies for you.

散發具有原生相依性的應用程式時,您可能需要使用 dotnet publish -r <RID> 參數來指定想要為其發佈的目標平台。When distributing an app with native dependencies, you may need to use the dotnet publish -r <RID> switch to specify the target platform you want to publish for. 如需執行階段識別碼清單,請參閱執行階段識別碼 (RID) 目錄For a list of runtime identifiers, see Runtime Identifier (RID) catalog.

平台特定二進位檔的詳細資訊涵蓋在 Framework 相依可執行檔獨立式部署章節中。More information about platform-specific binaries is covered in the Framework-dependent executable and Self-contained deployment sections.

範例應用程式Sample app

您也可以使用下列應用程式來瀏覽發佈命令。You can use either the following app to explore the publishing commands. 在終端機中執行下列命令即可建立此應用程式:The app is created by running the following commands in your terminal:

mkdir apptest1
cd apptest1
dotnet new console
dotnet add package Figgle

主控台範本所產生的 Program.csProgram.vb 檔案必須變更如下:The Program.cs or Program.vb file that is generated by the console template needs to be changed to the following:

using System;

namespace apptest1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Figgle.FiggleFonts.Standard.Render("Hello, World!"));
        }
    }
}
Imports System

Module Program
    Sub Main(args As String())
        Console.WriteLine(Figgle.FiggleFonts.Standard.Render("Hello, World!"))
    End Sub
End Module

當您執行應用程式 (dotnet run) 時,即會顯示下列輸出:When you run the app (dotnet run), the following output is displayed:

  _   _      _ _         __        __         _     _ _
 | | | | ___| | | ___    \ \      / /__  _ __| | __| | |
 | |_| |/ _ \ | |/ _ \    \ \ /\ / / _ \| '__| |/ _` | |
 |  _  |  __/ | | (_) |    \ V  V / (_) | |  | | (_| |_|
 |_| |_|\___|_|_|\___( )    \_/\_/ \___/|_|  |_|\__,_(_)
                     |/

與 Framework 相依的部署Framework-dependent deployment

針對 .NET Core SDK 2.x CLI,Framework 相依部署 (FDD) 是基本 dotnet publish 命令的預設模式。For the .NET Core SDK 2.x CLI, framework-dependent deployment (FDD) is the default mode for the basic dotnet publish command.

當您將應用程式發佈為 FDD 時,就會在 ./bin/<BUILD-CONFIGURATION>/<TFM>/publish/ 資料夾中建立 <PROJECT-NAME>.dll 檔案。When you publish your app as an FDD, a <PROJECT-NAME>.dll file is created in the ./bin/<BUILD-CONFIGURATION>/<TFM>/publish/ folder. 若要執行您的應用程式,請巡覽至輸出資料夾,並使用 dotnet <PROJECT-NAME>.dll 命令。To run your app, navigate to the output folder and use the dotnet <PROJECT-NAME>.dll command.

您的應用程式會設定為以 .NET Core 特定版本為目標。Your app is configured to target a specific version of .NET Core. 該目標 .NET Core 執行階段必須位於您想要用來執行應用程式的電腦上。That targeted .NET Core runtime is required to be on the machine where you want to run your app. 例如,如果您的應用程式以 .NET Core 2.2 為目標,則應用程式執行所在的任何電腦都必須已安裝 .NET Core 2.2 執行階段。For example, if your app targets .NET Core 2.2, any machine that your app runs on must have the .NET Core 2.2 runtime installed. 依照發佈基本概念一節中所述,您可以編輯專案檔來變更預設的目標 Framework,或以多個 Framework 為目標。As stated in the Publishing basics section, you can edit your project file to change the default target framework or to target more than one framework.

發佈 FDD 會建立一個應用程式,以自動向前復原到應用程式執行所在系統上可用的最新 .NET Core 安全性修補程式。Publishing an FDD creates an app that automatically rolls-forward to the latest .NET Core security patch available on the system that runs the app. 如需編譯時期版本繫結的詳細資訊,請參閱選取要使用的 .NET Core 版本For more information on version binding at compile time, see Select the .NET Core version to use.

Framework 相依可執行檔Framework-dependent executable

針對 .NET Core SDK 3.x CLI,Framework 相依可執行檔 (FDE) 是基本 dotnet publish 命令的預設模式。For the .NET Core SDK 3.x CLI, framework-dependent executable (FDE) the default mode for the basic dotnet publish command. 只要您想要以目前的作業系統為目標,就不需要指定任何其他參數。You don't need to specify any other parameters as long as you want to target the current operating system.

在此模式中,將會建立平台特定可執行檔主機來裝載您的跨平台應用程式。In this mode, a platform-specific executable host is created to host your cross-platform app. 此模式類似於 FDD,因為 FDD 需要 dotnet 命令形式的主機。This mode is similar to FDD as FDD requires a host in the form of the dotnet command. 主機可執行檔的檔名因平台而異,且其名稱類似於 <PROJECT-FILE>.exeThe host executable filename varies per platform, and is named something similar to <PROJECT-FILE>.exe. 您可以直接執行這個可執行檔,而不是呼叫 dotnet <PROJECT-FILE>.dll (這仍然是可接受的應用程式執行方式)。You can run this executable directly instead of calling dotnet <PROJECT-FILE>.dll which is still an acceptable way to run the app.

您的應用程式會設定為以 .NET Core 特定版本為目標。Your app is configured to target a specific version of .NET Core. 該目標 .NET Core 執行階段必須位於您想要用來執行應用程式的電腦上。That targeted .NET Core runtime is required to be on the machine where you want to run your app. 例如,如果您的應用程式以 .NET Core 2.2 為目標,則應用程式執行所在的任何電腦都必須已安裝 .NET Core 2.2 執行階段。For example, if your app targets .NET Core 2.2, any machine that your app runs on must have the .NET Core 2.2 runtime installed. 依照發佈基本概念一節中所述,您可以編輯專案檔來變更預設的目標 Framework,或以多個 Framework 為目標。As stated in the Publishing basics section, you can edit your project file to change the default target framework or to target more than one framework.

發佈 FDE 會建立一個應用程式,以自動向前復原到應用程式執行所在系統上可用的最新 .NET Core 安全性修補程式。Publishing an FDE creates an app that automatically rolls-forward to the latest .NET Core security patch available on the system that runs the app. 如需編譯時期版本繫結的詳細資訊,請參閱選取要使用的 .NET Core 版本For more information on version binding at compile time, see Select the .NET Core version to use.

您必須 (當您以目前平台為目標時,.NET Core 3.x 除外) 使用下列參數搭配 dotnet publish 命令來發佈 FDE:You must (except for .NET Core 3.x when you target the current platform) use the following switches with the dotnet publish command to publish an FDE:

  • -r <RID> 此參數會使用識別碼 (RID) 來指定目標平台。This switch uses an identifier (RID) to specify the target platform. 如需執行階段識別碼清單,請參閱執行階段識別碼 (RID) 目錄For a list of runtime identifiers, see Runtime Identifier (RID) catalog.

  • --self-contained false 此參數會指示 .NET Core SDK 將可執行檔建立為 FDE。--self-contained false This switch tells the .NET Core SDK to create an executable as an FDE.

每當您使用 -r 參數時,輸出資料夾路徑就會變更為:Whenever you use the -r switch, the output folder path changes to: ./bin/<BUILD-CONFIGURATION>/<TFM>/<RID>/publish/

如果您使用範例應用程式,請執行 dotnet publish -f netcoreapp2.2 -r win10-x64 --self-contained falseIf you use the example app, run dotnet publish -f netcoreapp2.2 -r win10-x64 --self-contained false. 此命令會建立下列可執行檔:This command creates the following executable: ./bin/Debug/netcoreapp2.2/win10-x64/publish/apptest1.exe

注意

您可以啟用全域無差異模式來減少您部署的大小總計。You can reduce the total size of your deployment by enabling globalization invariant mode. 此模式適用於非全域應用程式,其能使用格式化慣例、大小寫慣例及字串比較,還有不因文化特性而異的排序次序。This mode is useful for applications that are not globally aware and that can use the formatting conventions, casing conventions, and string comparison and sort order of the invariant culture. 如需全域無差異模式和如何啟用的詳細資訊,請參閱 .NET Core 全域無差異模式For more information about globalization invariant mode and how to enable it, see .NET Core Globalization Invariant Mode

自封式部署Self-contained deployment

當您發佈獨立式部署 (SCD) 時,.NET Core SDK 會建立平台特定的可執行檔。When you publish a self-contained deployment (SCD), the .NET Core SDK creates a platform-specific executable. 發佈 SCD 包含執行應用程式所需的所有 .NET Core 檔案,但它不包含 的原生相依性Publishing an SCD includes all required .NET Core files to run your app but it doesn't include the native dependencies of .NET Core. 在執行應用程式之前,系統上必須具有這些相依性。These dependencies must be present on the system before the app runs.

發佈 SCD 會建立一個應用程式,該應用程式不會向前復原到最新可用的 .NET Core 安全性修補程式。Publishing an SCD creates an app that doesn't roll-forward to the latest available .NET Core security patch. 如需編譯時期版本繫結的詳細資訊,請參閱選取要使用的 .NET Core 版本For more information on version binding at compile time, see Select the .NET Core version to use.

您必須使用下列參數搭配 dotnet publish 命令來發佈 SCD:You must use the following switches with the dotnet publish command to publish an SCD:

  • -r <RID> 此參數會使用識別碼 (RID) 來指定目標平台。This switch uses an identifier (RID) to specify the target platform. 如需執行階段識別碼清單,請參閱執行階段識別碼 (RID) 目錄For a list of runtime identifiers, see Runtime Identifier (RID) catalog.

  • --self-contained true 此參數會指示 .NET Core SDK 將可執行檔建立為 SCD。--self-contained true This switch tells the .NET Core SDK to create an executable as an SCD.

注意

您可以啟用全域無差異模式來減少您部署的大小總計。You can reduce the total size of your deployment by enabling globalization invariant mode. 此模式適用於非全域應用程式,其能使用格式化慣例、大小寫慣例及字串比較,還有不因文化特性而異的排序次序。This mode is useful for applications that are not globally aware and that can use the formatting conventions, casing conventions, and string comparison and sort order of the invariant culture. 如需全域無差異模式和如何啟用的詳細資訊,請參閱 .NET Core 全域無差異模式For more information about globalization invariant mode and how to enable it, see .NET Core Globalization Invariant Mode

另請參閱See also