.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 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-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 命令Command
适用于当前平台的依赖于框架的可执行文件framework-dependent executable for the current platform. ✔️✔️ dotnet publish
适用于特定平台的依赖于框架的可执行文件framework-dependent executable for a specific platform. ✔️✔️ dotnet publish -r <RID> --self-contained false
依赖于框架的跨平台二进制文件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. 发布应用并创建可执行文件时,可以将应用发布为独立应用依赖于框架的应用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. 如果将应用发布为依赖于框架的应用,则该应用不包含 .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 命令Command
适用于当前平台的依赖于框架的可执行文件framework-dependent executable for the current platform. ✔️✔️ dotnet publish
适用于特定平台的依赖于框架的可执行文件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

以 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. 有关详细信息,请参阅依赖于框架的应用前滚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 命令Command
依赖于框架的跨平台二进制文件framework-dependent cross-platform binary. ✔️✔️ ✔️✔️ dotnet publish

发布依赖于框架的应用Publish framework-dependent

如果将应用发布为依赖于框架的应用,则该应用是跨平台的,且不包含 .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.

如果将应用发布为依赖于框架的应用,会以 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.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 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 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 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. 有关详细信息,请参阅依赖于框架的应用前滚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. 有关详细信息,请参阅依赖于框架的应用前滚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 不会生成特定于平台的可执行文件。.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. 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 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