.NET CORE 9 在Centos 7上编译Native Aot
最近在做云服务迁移技术服务,为了自己软件的安全不想给客户看到咱们写的程序源代码,而且又不知道客户服务器的环境,那么只能同时生成Windows和Linux的Aot版本的应用程序。
本次框架直接选型最新的.NET CORE 9.0,这个文章不是为了科普而是为了自己记录一下整个发布过程而已
1、安装.net core sdk
通过 wget 下载脚本
wget https://dot.net/v1/dotnet-install.sh -O dotnet-install.sh
授予此脚本作为可执行文件运行的权限
chmod +x ./dotnet-install.sh
安装最新的LTS版本SDK(它默认安装的是.NET 8.0 SDK)
./dotnet-install.sh --version latest
可以通过 --channel 参数更改特定主要版本来指示特定版本。 以下命令安装 .NET 9.0 SDK。
./dotnet-install.sh --channel 9.0
经过一段时间的执行,会显示如下信息:
dotnet-install: Attempting to download using aka.ms link https://builds.dotnet.microsoft.com/dotnet/Sdk/9.0.300/dotnet-sdk-9.0.300-linux-x64.tar.gz dotnet-install: Remote file https://builds.dotnet.microsoft.com/dotnet/Sdk/9.0.300/dotnet-sdk-9.0.300-linux-x64.tar.gz size is 217847129 bytes. dotnet-install: Extracting archive from https://builds.dotnet.microsoft.com/dotnet/Sdk/9.0.300/dotnet-sdk-9.0.300-linux-x64.tar.gz dotnet-install: Downloaded file size is 217847129 bytes. dotnet-install: The remote and local file sizes are equal. dotnet-install: Installed version is 9.0.300 dotnet-install: Adding to current process PATH: `/root/.dotnet`. Note: This change will be visible only when sourcing script. dotnet-install: Note that the script does not resolve dependencies during installation. dotnet-install: To check the list of dependencies, go to https://learn.microsoft.com/dotnet/core/install, select your operating system and check the "Dependencies" section. dotnet-install: Installation finished successfully.
当我们看到 Installation finished successfully后,那表示我们安装成功啦!接下来就要编译程序了
哦,差点忘了,还要设置一下.NET 的环境,执行这两段命令
export DOTNET_ROOT=$HOME/.dotnet
export PATH=$PATH:$DOTNET_ROOT:$DOTNET_ROOT/tools
接下来,我们验证一下dotnet安装情况
dotnet
如果显示下面这样,那么就是环境完全配置OK了
Usage: dotnet [options] Usage: dotnet [path-to-application] Options: -h|--help Display help. --info Display .NET information. --list-sdks Display the installed SDKs. --list-runtimes Display the installed runtimes. path-to-application: The path to an application .dll file to execute.
接下来,将咱们的项目通过ssh上传至centos 7的服务器或虚拟机
使用命令进入到项目文件夹,再执行下面的命令:
dotnet publish -c Release -r linux-x64 --self-contained true -p:PublishAot=true /p:TieredCompilation=false /p:ServerGarbageCollection=true /p:TrimMode=Link /p:PublishSingleFile=true /p:PublishTrimmed=true /p:EnableCompressionInSingleFile=true /p:DebugType=None /p:DebugSymbols=false
接下来就是等待了,它会自动开始编译,因为这次的程序特殊而且比较小,所以速度还是很快的(至少我觉得比在Windows上快多啦)
欢迎使用 .NET 9.0! --------------------- SDK 版本: 9.0.300 遥测 --------- .NET 工具会收集用法数据,帮助我们改善你的体验。它由 Microsoft 收集并与社区共享。你可通过使用喜欢的 shell 将 DOTNET_CLI_TELEMETRY_OPTOUT 环境变量设置为 "1" 或 "true" 来选择退出遥测。 阅读有关 .NET CLI 工具遥测的更多信息: https://aka.ms/dotnet-cli-telemetry ---------------- 已安装 ASP.NET Core HTTPS 开发证书。 若要信任该证书,请运行 "dotnet dev-certs https --trust" 了解 HTTPS: https://aka.ms/dotnet-https ---------------- 编写第一个应用: https://aka.ms/dotnet-hello-world 了解新增功能: https://aka.ms/dotnet-whats-new 浏览文档: https://aka.ms/dotnet-docs 报告问题并在 GitHub 上查找来源: https://github.com/dotnet/core 使用 "dotnet --help" 查看可用命令或访问: https://aka.ms/dotnet-cli -------------------------------------------------------------------------------------- 还原完成(9.9) xxxxxx 已成功 (13.9 秒) → bin/Release/net9.0/linux-x64/publish/ 在 24.3 秒内生成 已成功
上面操作完成后,在 bin/Release/net9.0/linux-x64/publish/里面的文件就是最终生成的.NET 9 Native AOT应用程序咯,再往后的部署方法就不描述了,大家可以百度一下怎么在服务器上执行,必竟这并不属于编译相关的内容,所以我就不再描述了。

浙公网安备 33010602011771号