【.NET Core】在树莓派Raspberry Pi 3b+上运行.net core 2.1程序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/catshitone/article/details/82084131

前言

最近想做个私有NAS,所以入手了一块树莓派 3b+,刷了openmediavault系统。系统是基于linux的,然后寻思着看树莓派能不能运行下.netcore程序。经过一阵搜索,发现其实步骤还是挺简单的。

总结下就是:

  1. 在你的树莓派上安装linux系统。现在支持arm32的linux系统我知道的有Debian和Ubuntu。openmediavalut和树莓派官方的Raspbian都是基于Debian的。
  2. 安装.netcore在对应系统上的一些依赖。
  3. publish你的程序到对应的平台(linux-arm),然后拷贝到树莓派上。

实现

1. 在树莓派上安装linux系统

因为之前已经安装过openmediavault了,所以跳过。如果想安装的话,可以点击下载
这里写图片描述
安装的话,就按照页面下方的文字说明进行安装即可。亦可自行百度。

2.安装相关依赖

.netcore开发团队已经给我们写了详细的安装依赖步骤:
这里写图片描述
反正都是基于debian的系统,我就选择了上图的两个命令。其它系统的相关依赖详见:依赖

Ubuntu
Ubuntu distributions require the following libraries installed:
liblttng-ust0
libcurl3
libssl1.0.0
libkrb5-3
zlib1g
libicu52 (for 14.x)
libicu55 (for 16.x)
libicu57 (for 17.x)
libicu60 (for 18.x)
For versions earlier than .NET Core 2.1, following dependencies are also required:
libunwind8
libuuid1
CentOS
CentOS distributions require the following libraries installed:
lttng-ust
libcurl
openssl-libs
krb5-libs
libicu
zlib
For versions earlier than .NET Core 2.1, following dependencies are also required:
libunwind
libuuid
For more information about the dependencies, see Self-contained Linux applications.

3.编写.NET Core程序

这里我用VS2017写了一个core 2.1的控制台应用ConsolePi,很简单没什么功能,就是输出一句话。

class Program
{
   static void Main(string[] args)
   {
        Console.WriteLine("Hello from pi!");
        Console.Read();
   }
}

4.发布、上传并执行

cmd或者powershell到你csproj文件所在的目录,输入以下命令:

dotnet publish -r linux-arm

将其发布到linux-arm平台。这是发布后的路径和一些文件(183个):
这里写图片描述
打开XShell,连接上树莓派(需要先把ssh打开):
这里写图片描述
然后输入以下命令在当前目录新建一个publish文件夹:

mkdir publish

这里写图片描述
打开XShell的ftp工具:
这里写图片描述
把之前发布好的那一百多个文件,拖到右边的publish文件夹里:
这里写图片描述
等待上传完成后,在xshell里cd到publish目录,然后执行chmod 777 ConsolePi命令,赋予ConsolePi文件相应的权限。然后输入./ConsolePi命令,不出意外,你应该能够看到Hello from pi了。
这里写图片描述

5. 下一步工作

等接下来的装备到齐了,在试着调用下树莓派的GPIO。


参考

1 .NET Core on Raspberry Pi
2. Prerequisites for .NET Core on Linux
3. 树莓派3中运行Netcore2.0程序
4. Running a .NET Core 2 app on Raspbian Jessie, and deploying to the Pi with Cake

猜你喜欢

转载自blog.csdn.net/catshitone/article/details/82084131