Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run ASP.NET Core on arm-based custom linux?

I want to build an ASP.NET Core Web API service to run on an arm (cortex-A7) device with an angstrom based linux.

Question: What is the best route to follow?

  1. Compile CoreClr, CoreFx, and what-not for the arm7 device (could be possible, but I'm not sure about the angstrom linux). If that works, published bytecode should run on that device.
  2. cross-compile my aspnet app using dotnet native and target arm. I couldn't find useful infos but it could be possible.
  3. Something else? Like generate C++ code from my project?
  4. Compile a different linux distro for the device (probably a no-go since we need IPC between the Linux Cortex-A7 CPU and the FreeRTOS Cotex-M4 CPU).

My main concern is the angstrom distribution, which is not supported by dotnet core out of the box. Any advice is welcome!


Edit, Open Questions:

  • Where can I download the correct pre-built dotnet binaries for an Cortex-A7 (I could not get https://github.com/dotnet/core-setup to work)?
  • What other prerequisites do I need (if any)?
like image 842
Patrick Stalph Avatar asked May 12 '17 07:05

Patrick Stalph


2 Answers

You can use the portable linux-arm runtime build of .NET Core. On your build machine, make sure that dotnet --version returns a 2.0, preview, or higher version (at the time of writing: 2.0.0-preview1-005977).

  1. dotnet new mvc
  2. dotnet restore -r linux-arm
  3. dotnet publish -r linux-arm /p:MvcRazorCompileOnPublish=false
  4. Copy the contents of bin/Debug/netcoreapp2.0/linux-arm/publish to your target machine and run with ./nameOfTheProject

Since you probably want to develop locally as well, you'd want to edit the project (.csproj) file like this (<PropertyGroup>):

<RuntimeIdentifiers>linux-arm</RuntimeIdentifiers>
<MvcRazorCompileOnPublish Condition=" '$(RuntimeIdentifier)' != 'linux-arm' ">true</MvcRazorCompileOnPublish>

This way you can use both dotnet restore, dotnet run and dotnet publish during development or other deploys without additional parameters and when ready to deploy to arm, only use:

dotnet publish -c Release -r linux-arm

and use the resulting binaries from bin/Release/netcoreapp2.0/linux-arm/publish (or pass an additional -o ../publish-output argument)

like image 78
Martin Ullrich Avatar answered Nov 01 '22 14:11

Martin Ullrich


Based on Martin Ulrichs suggestions I had partial success in cross-compiling a self-contained executable for arm. The code is cross-compiled on a Ubuntu.16.04-x64 machine and runs on a raspberry pi 2 (GNU/Linux 4.1.17-v7+ armv7l) without installing the dotnet runtime on the PI.

On the build machine, Ubuntu.16.04-x64 with dotnet --version = 2.0.0-preview1-005977, run the following:

mkdir foobar; cd foobar
dotnet new console
dotnet restore -r linux-arm
dotnet publish -r linux-arm

This produces a 31MB publish folder with 174 files. Copy those over to the arm device. Note that the resulting executable will not run on the build machine (wrong architecture).

On the arm device

First, you have to install some required libraries for the dotnet runtime. Unfortunatley this is debian, and probably not suitable for angstrom (this is why I don't mark this as answer). I picked only non-dev prerequisites from here, since the app is not compiled on the PI.

sudo apt-get install libunwind8 libicu52 gettext liblttng-ust-ctl2 liblttng-ust0

This list may contain too much / not enough depending on the actual application. Finally, run the self-contained application

chmod +x bin/Debug/netcoreapp2.0/linux-arm/publish/foobar
bin/Debug/netcoreapp2.0/linux-arm/publish/foobar

The execution speed (slow as hell for writing "Hello World!" to the console) indicates that this is actually byte code bootstrapped by some kind of included dotnet clr.

like image 26
Patrick Stalph Avatar answered Nov 01 '22 14:11

Patrick Stalph