In this article we will:
various ways to publish a .NET 5.0 application.
Publishing A Single EXE File In .NET
Table of Contents
ASP.NET Core的四种部署方式
NET Core有三种部署方式:依赖框架的部署(FDD),独立部署(SCD)和依赖框架的可执行文件(FDE)
第一种是跨平台包,需要预先安装.NET Core环境。
第二种是SCD包(包名带"scd")-self-contained
无需安装.net环境,用户需要根据自己的平台和架构选择相应的压缩包。
第三种是Windows窗体版本(包名带"winform")
Native AOT deployment
1.独立部署(SCD)-self-contained
无需安装.net环境。
.NET Core 提供的发布应用程序选项 self-contained 是共享应用程序的好方法,因为应用程序的发布目录包含所有组件、运行时和框架。您只需要告诉使用者应用程序的入口 exe 文件,就可以使程序运行起来,而不必担心目标计算机上是否存在.NET Core 运行时和应用框架。
.Net 8.x
Native AOT deployment
<PropertyGroup>
<PublishAot>true</PublishAot>
</PropertyGroup>
Native AOT deployment
source:https://learn.microsoft.com/zh-cn/dotnet/core/deploying/native-aot/?tabs=net8plus%2Cwindows
ASP.NET Core 8.0 introduces support for .NET native ahead-of-time (AOT).
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/native-aot?view=aspnetcore-8.0
Raspberry Pi Raspberry Pi OS
dotnet publish --runtime linux-arm64 --self-contained
Windows.
dotnet publish -c Release -r win-x64 --self-contained
https://github.com/net-daemon/micro_netdaemon
NetCoreTemplates/selfhost: .NET 8 self-hosting Console
https://github.com/NetCoreTemplates/selfhost
.Net 6.x
.NET 6.0 produces the true single file
the dotnet6.0 is generatring the true single file. That means, developers can build the application as a self-contained and all framework dependent assemblies can be compiled into the single file. Now, all of them are linked together and if wanted trimmed.
To create a such build, developers can use Visual Studio 2022 or to use the project file. Following figure shows all required settings in the VS.
Following snippet shows the configuration of the project file that can be used to produce the single file.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<PublishSingleFile>true</PublishSingleFile>
<SelfContained>true</SelfContained>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<PublishReadyToRun>true</PublishReadyToRun>
</PropertyGroup>
</Project>
https://developers.de/2021/11/08/net-6-0-produces-the-true-single-file/
.Net 5.x
1. Self-contained executable
dotnet (and core) have been collecting the application intyhe single executable host file, the framework in another dll and the runtime in another dll.
dotnet publish -c Release -r <RID> --self-contained true
dotnet publish -c Release -r win-x64 --self-contained true
output
D:\tmp\WebApplication1\WebApplication1\bin\Release\netcoreapp3.1\win-x64\publish
vs 2019 create .exe
https://zhuanlan.zhihu.com/p/338811207
修改配置--部署模式 依赖框架,目标运行时选对应平台,文件发布选项 勾选生成单个文件
https://docs.microsoft.com/en-us/dotnet/core/deploying/single-file/overview
2.依赖框架的部署(FDD)
无需系统安装.NET Core库,生成的应用包含了.NET Core库,还包括一个可执行文件,dll文件
3.依赖框架的可执行文件(FDE)
系统需安装的.NET Core库但是有可执行文件
.NET 5.x
Ways to publish a .NET 5.0 application with the.NET command
Framework-dependent deployment
Framework-dependent deployment produces a cross-platform .dll file that uses the locally installed .NET runtime.
dotnet publish -c Release
Framework-dependent executable
Framework-dependent executable produces a platform-specific executable that uses the locally installed .NET runtime.
dotnet publish -c Release -r win-x64 --self-contained false
Self-contained executable
Self-contained executable produces a platform-specific executable and includes a local copy of the .NET Core runtime and BCL.
dotnet publish -c Release -r win-x64 --self-contained false
Self-contained single file executable
Self-contained single file executable produces a platform-specific executable and embeds a local copy of the .NET Core runtime in the executable.
dotnet publish HelloWorld -c Release -r win-x64 --self-contained true /p:PublishSingleFile=true
.NET 3.x
Useful links
.Net 6.x
https://docs.microsoft.com/en-us/dotnet/core/deploying/single-file
https://docs.microsoft.com/en-us/dotnet/core/deploying/#self-contained-deployments-scd
.Net 5.x
various ways to publish a .NET 5.0 application
Office doc:
https://docs.microsoft.com/en-us/dotnet/core/deploying/deploy-with-cli