1).简介
嵌入式Linux逐渐成为嵌入式设备的主流操作系统,但是应用开发工程师很多更熟悉Microsoft C#开发,得益于Microsoft .Net Framework的跨平台支持,本文就简单示例在Embedded Linux环境运行.NET应用。
本文所演示的平台来自于ToradexApalisiMX8嵌入式平台,基于NXP iMX8系列ARM处理器,核心为Cortex-A52/A53。
2).硬件准备
a).Apalis iMX8 ARM核心版配合Apalis Eva Board载板,并连接调试串口和网口以便测试。
3).部署Microsoft .Net Framework运行库到Embedded Linux BSP
a). Apalis iMX8模块标准Ycoto Linux BSP中默认没有包含Microsoft .Net Framework相关runtime运行库支持,可以参考这里Microsoft官方文档通过script方式安装,不过为了方便管理和部署,本文演示通过Ycoto Project/Openembedded编译方式来添加相关库文件,Ycoto Project .Net Framework runtime由下面meta-dotnet-core layer来提供,详细说明可以参考项目README的说明。
https://github.com/RDunkley/meta-dotnet-core
b).首先需要在Linux编译开发主机部署Ycoto编译环境,然后参考如下步骤部署。
./下载meta-dotnet-core layer
---------------------------------------
$ cd .../oe_core/layers
$ git clone https://github.com/RDunkley/meta-dotnet-core.git
---------------------------------------
./修改build/conf/bblayer.conf文件添加相关layer
---------------------------------------
---a/build/conf/bblayers.conf2023-03-30 11:13:22.946533642 +0800
+++b/build/conf/bblayers.conf2023-11-17 16:03:01.666129480 +0800
@@ -35,6 +35,7 @@
${TOPDIR}/../layers/meta-freescale-distro \
${TOPDIR}/../layers/meta-toradex-demos \
${TOPDIR}/../layers/meta-qt5 \
+ ${TOPDIR}/../layers/meta-dotnet-core \
\
\
${TOPDIR}/../layers/meta-toradex-distro \
---------------------------------------
./修改build/conf/local.conf文件,增加.NET相关库组件,如果需要可以选择指定.NET runtime版本,本文编译测试没有指定,默认是支持的最高版本,当前是7.0.11版本
---------------------------------------
# add dotnet runtime
IMAGE_INSTALL:append = " dotnet-core"
# Optional - specify dotnet runtime version
#PREFERRED_VERSION_dotnet-core = "6.0.0"
---------------------------------------
当然也可以选择添加ASP .NET (aspnet-core)和Visual Studio Remote Debugger (vsdbg)组件,需要注意ASP .NET runtime会包含.NET runtime,因此两者不要同时添加
---------------------------------------
# add asp dotnet runtime
IMAGE_INSTALL:append = " aspnet-core"
# add VS remote debugger
IMAGE_INSTALL:append = " vsdbg"
---------------------------------------
./重新编译生成Ycoto Linux BSP Image
---------------------------------------
$ MACHINE="apalis-imx8" PARALLEL_MAKE="-j 4" BB_NUMBER_THREADS="4" bitbake tdx-reference-multimedia-image
---------------------------------------
c).参考这里的说明将上述修改下重新编译生成的Ycoto Linux Image通过Toradex Easy Installer更新到Apalis iMX8模块。
4).生成.NET 7.0 Console测试应用
a).可以通过Virtual Studio或者Virtual Studio Code IDE来生成这个Hello World Console应用,本文参考这里的说明使用VS Code来生成,VS Code Terminal操作如下
---------------------------------------
$ dotnet --list-sdks
7.0.114 [/usr/lib/dotnet/sdk]
$ dotnet new console -f net7.0
欢迎使用.NET 7.0!
---------------------
SDK版本: 7.0.114
----------------
已安装ASP.NET Core HTTPS开发证书。
若要信任该证书,请运行"dotnet dev-certs https --trust" (仅限Windows和macOS)。
了解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
--------------------------------------------------------------------------------------
已成功创建模板“控制台应用”。
正在处理创建后操作...
正在还原/home/simon/local/project/vscode/DotNetExample/DotNetExample.csproj:
Determining projects to restore...
Restored /home/simon/local/project/vscode/DotNetExample/DotNetExample.csproj (in 95 ms).
已成功还原。
$ dotnet run
Hello, World!
$ dotnet publish
MSBuild version 17.4.8+6918b863a for .NET
Determining projects to restore...
All projects are up-to-date for restore.
DotNetExample -> /home/simon/local/project/vscode/DotNetExample/bin/Debug/net7.0/DotNetExample.dll
DotNetExample -> /home/simon/local/project/vscode/DotNetExample/bin/Debug/net7.0/publish/
---------------------------------------
5).在Apalis iMX8 Embedded Linux下测试.NET 7.0 Console应用
a).通过scp将publish的.NET 7.0应用传输到Apalis iMX8模块
./ Apalis iMX8测试上面章节3部署的.NET runtime环境
---------------------------------------
root@apalis-imx8-06738378:~# dotnet --info
Host:
Version: 7.0.11
Architecture: arm64
Commit: ecb34f85ec
.NET SDKs installed:
No SDKs were found.
.NET runtimes installed:
Microsoft.NETCore.App 7.0.11 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
Other architectures found:
None
Environment variables:
Not set
global.json file:
Not found
Learn more:
https://aka.ms/dotnet/info
Download .NET:
https://aka.ms/dotnet/download
---------------------------------------
./将示例.NET应用从开发主机复制到Apalis iMX8
---------------------------------------
### create app folder on Apalis iMX8 ###
root@apalis-imx8-06738378:~# cd ~
root@apalis-imx8-06738378:~# mkdir DotNetExample
### scp published app to apalis iMX8 ###
$ scp -r /home/simon/local/project/vscode/DotNetExample/bin/Debug/net7.0/publish/* root@:/home/root/DotNetExample
---------------------------------------
b).运行测试应用
---------------------------------------
root@apalis-imx8-06738378:~# cd DotNetExample/
root@apalis-imx8-06738378:~/DotNetExample# dotnet DotNetExample.dll
Hello, World!
---------------------------------------
6).总结
本文基于嵌入式Linux简单演示了.NET runtime的部署和示例应用运行。