.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.

将应用发布为依赖于运行时的应用,生成的应用程序仅包含该应用程序本身及其依赖项。Publishing your app as runtime-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. 不使用可执行文件也可以创建依赖于运行时的应用程序,这些应用程序是跨平台的。Runtime-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 runtime-dependent or self-contained, per SDK version:

类型Type SDK 2.1SDK 2.1 SDK 3.xSDK 3.x 命令Command
适用于当前平台的依赖于运行时的可执行文件runtime-dependent executable for the current platform. ✔️✔️ dotnet publish
适用于特定平台的依赖于运行时的可执行文件runtime-dependent executable for a specific platform. ✔️✔️ dotnet publish -r <RID> --self-contained false
依赖于运行时的跨平台二进制文件runtime-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. 发布应用并创建可执行文件时,可以将应用发布为独立应用依赖于运行时的应用When publishing your app and creating an executable, you can publish the app as self-contained or runtime-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. 如果将应用发布为依赖于运行时的应用,则该应用不包含 .NET Core 运行时和库,而仅包含该应用和第三方依赖项。Apps published as runtime-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 命令Command
适用于当前平台的依赖于运行时的可执行文件runtime-dependent executable for the current platform. ✔️✔️ dotnet publish
适用于特定平台的依赖于运行时的可执行文件runtime-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

以 dll 文件的形式将应用发布为依赖于运行时的应用时,将创建跨平台二进制文件。Cross-platform binaries are created when you publish your app as runtime-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. 有关详细信息,请参阅依赖于运行时的应用前滚For more information, see runtime-dependent apps roll forward.

以下命令可生成跨平台二进制文件:The following command produces a cross-platform binary:

类型Type SDK 2.1SDK 2.1 SDK 3.xSDK 3.x 命令Command
依赖于运行时的跨平台二进制文件runtime-dependent cross-platform binary. ✔️✔️ ✔️✔️ dotnet publish

发布依赖于运行时的应用Publish runtime-dependent

如果将应用发布为依赖于运行时的应用,则该应用是跨平台的,且不包含 .NET Core 运行时。Apps published as runtime-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.

将应用发布为依赖于运行时的应用,会以 dll 文件的形式生成一个 跨平台二进制文件,还会生成面向当前平台的特定于平台的可执行文件Publishing an app as runtime-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.exe 和 word_reader.dll。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.dll。When 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 runtime-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 publish 命令,为特定平台创建可执行文件。You 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
    应用和任何基于 .NET 的库都可在不同的操作系统上运行。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. 有关详细信息,请参阅依赖于运行时的应用前滚For more information, see runtime-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. 有关详细信息,请参阅依赖于运行时的应用前滚For more information, see runtime-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. 有关详细信息,请参阅依赖于运行时的应用前滚For more information, see runtime-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 不会生成特定于平台的可执行文件。.NET Core 2.1 SDK doesn't produce platform-specific executables for apps published runtime-dependent.

示例Examples

发布依赖于运行时的跨平台应用。Publish an app cross-platform runtime-dependent. dll 文件一起创建面向当前平台的可执行文件。An executable that targets your current platform is created along with the dll file.

dotnet publish

发布依赖于运行时的跨平台应用。Publish an app cross-platform runtime-dependent. dll 文件一起创建 Linux 64 位可执行文件。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. dotnet publish 命令的 -r <RID> 参数指定目标平台和体系结构。The 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 运行时和所有应用依赖项,因此下载大小和所需硬盘空间比依赖于运行时的版本大。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 runtime-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.

  • 较难更新 .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 运行时的安全修补程序提供应用程序的更新版本。You're responsible for supplying an updated version of your application for security patches to the .NET Core Runtime.

示例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

请参阅See also