惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
Check Point Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 聂微东
月光博客
月光博客
博客园 - 司徒正美
爱范儿
爱范儿
aimingoo的专栏
aimingoo的专栏
量子位
Recent Announcements
Recent Announcements
V
V2EX
P
Proofpoint News Feed
小众软件
小众软件
云风的 BLOG
云风的 BLOG
腾讯CDC
宝玉的分享
宝玉的分享
Microsoft Azure Blog
Microsoft Azure Blog
大猫的无限游戏
大猫的无限游戏
Vercel News
Vercel News
The GitHub Blog
The GitHub Blog
A
About on SuperTechFans
B
Blog
博客园_首页
GbyAI
GbyAI
博客园 - Franky

博客园 - love/coder

Android studio环境配置 phpstorm/clion设置文件头部版权信息 php7.4需要升级vc++版本问题 SqlServer查询所有表名及其数据行数 图解 CSS 选择器 C# 子窗体中调用父窗体中的方法(或多窗体之间方法调用) Winform控件命名规则 JQuery UI实现浮动客服并拨号 C#中的Timer定时器 winform中的多线程与异步加载 base64的使用 编程命名规范(网文) 常浏览编程技术资源 接口控制器层(Controller层)设计(网文) SpringBoot编写WebApi~(1)idea创建项目并打包 使用FileBrowser搭建文件服务 VS新建文件时在头部附加版权信息 记录一次Apache2.4启动PHP的Curl扩展不成功的问题 FileZilla Server搭建ftp服务 VS安装插件,按CTRL+鼠标左键进入函数
.NET8的AOT技术
love/coder · 2024-07-24 · via 博客园 - love/coder

AOT技术的好处:

提升启动速度;

降低发布包的大小;

内存占用小;

编译为字节码反编译难度加大; 

环境要求

vs2022、安装的时候勾选c++桌面开发,新版.net sdk等。

 项目配置

<PropertyGroup>
  <RuntimeIdentifier>win-x86;win-x64;osx-x64;linux-x64</RuntimeIdentifier> <!-- 根据需要设置 -->
  <PublishSingleFile>true</PublishSingleFile>
  <PublishTrimmed>true</PublishTrimmed> <!-- 可选,启用去除未使用的库 -->
  <PublishReadyToRun>true</PublishReadyToRun> <!-- 启用ReadyToRun -->
  <IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract> <!-- 包含本地依赖 -->

<PublishAot>true</PublishAot> <!-- 启用 AOT 编译 -->
<_SuppressWinFormsTrimError>true</_SuppressWinFormsTrimError>
<SelfContained>true</SelfContained> <!-- 自包含发布(推荐) -->

</PropertyGroup>

注:winfrom项目安装WinFormsComInterop

常规发布命令

dotnet publish -r linux-arm64 -c Release

 

AOT发布(-p:PublishAot=true 参数)

dotnet publish -r linux-arm64 -c Release -p:PublishAot=true

然后通过以下命令发布项目,生成链接库:


dotnet publish -p:NativeLib=Shared -r win-x64 -c Release

指定项目

dotnet publish -p:Project=./MyProject/MyProject.csproj

指定解决方案

dotnet publish ./MySolution.sln

 

体积裁剪配置

在csproj 文件加入以下配置:

<!--AOT 相关-->

< PublishAot> true </ PublishAot>

< TrimMode> full </ TrimMode>

< RunAOTCompilation> True </ RunAOTCompilation>

< PublishTrimmed> true </ PublishTrimmed>

< TrimmerRemoveSymbols> true </ TrimmerRemoveSymbols>

< PublishReadyToRunEmitSymbols> false </ PublishReadyToRunEmitSymbols>

< DebuggerSupport> false </ DebuggerSupport>

< EnableUnsafeUTF7Encoding> true </ EnableUnsafeUTF7Encoding>

< InvariantGlobalization> true </ InvariantGlobalization>

< HttpActivityPropagationSupport> false </ HttpActivityPropagationSupport>

< MetadataUpdaterSupport> true </ MetadataUpdaterSupport>

< UseSystemResourceKeys> true </ UseSystemResourceKeys>

< IlcDisableReflection> true </ IlcDisableReflection>

非aot调用aot库

使用函数导出,只是参数类型返回类型受限

aot调用aot库

参考:https://www.sohu.com/a/604546825_121124363