使用 Native AOT 发布应用程序会创建一个完全独立的应用程序版本,不需要运行时(所有内容都包含在一个文件中)。
.NET 7 中引入了作为 Native AOT 发布的选项,在 .NET 8 中持续改进
Native AOT 发布的 Hello World 应用程序大小:
OS | .NET 7 | .NET 8 Preview |
---|---|---|
Linux x64 (with -p:StripSymbols=true) | 3.76 MB | 1.84 MB |
Windows x64 | 2.85 MB | 1.77 MB |
Windows 需安装 VS2022 和 C++ 桌面开发
Ubuntu (18.04+) 需安装 sudo apt-get install clang zlib1g-dev
Alpine (3.15+) 需安装 sudo apk add clang build-base zlib-dev
添加该属性到项目文件
<PropertyGroup>
<PublishAot>true</PublishAot>
</PropertyGroup>
dotnet publish -r win-x64 -c Release
dotnet publish -r linux-arm64 -c Release
没安装 C++ 会报错
Microsoft.NETCore.Native.Windows.targets(116,5): error :
Platform linker not found. To fix this problem,
Make sure to install the Desktop Development for C++ workload.
For ARM64 development also install C++ ARM64 build tools.
在 Windows 发布 Linux 会提示不支持
Microsoft.NETCore.Native.Publish.targets(58,5): error :
Cross-OS native compilation is not supported.
.NET 8 跟进:
https://github.com/dotnet/runtime/issues/69739
https://github.com/dotnet/aspnetcore/issues/47240
https://github.com/dotnet/core/issues/8133
Native AOT:
https://learn.microsoft.com/zh-cn/dotnet/core/deploying/native-aot/