.Net, Azure and occasionally gamedev

Home App - .Net Core on a raspberry pi

2018/04/02

The previous homeapp already used a raspberry pi as its central server, however it was implemented as a plain old console application and required Mono on the raspberry pi.

This worked reasonably well except for the fact that the mono version shipped with Raspian by default is wholefully outdated and didn't support half the features I needed.

I was however able to install the latest mono by directly linking in their source "deb http://download.mono-project.com/repo/debian raspbianstretch main".

All of that is now however completely irrelevant because I'm switching my codebase to .Net Core/.Net Standard.

Configure the raspi

For .Net Core on a raspi the requirements are a little different (in fact a lot easier!).

First of all, ignore the official documentation for installation from here because it assumes you want to develop on the target machine (i.e. you want to install the SDK).

For the raspi this is not true (and also not supported).

The .Net Core team (rightfully) assumes that most people want to deploy their code to a raspi but not develop the code on the raspi itself. As such they have not invested time to port their SDK to ARM. If you develop on another linux distro you're most likely using x86 anyway and .Net Core SDK is supported there.

Since I don't plan to develop on the raspi the SDK is not needed and the installation becomes a lot shorter:

In order to get the code ready to be deployed on a raspi follow these instructions. Specifically the section "Getting the app to run on the Pi."

You'll notice that apart from updating you only have to install at most four dependencies via:

sudo apt-get install curl libunwind8 gettext apt-transport-https

That's it, now .Net Core apps should be able to run

Building the first project

I created a first "hello world" .Net Core 2.0 project and published it via

dotnet publish -r linux-arm --configuration Release

The "-r" will set the runtime identifier to publish the correct packages for a raspi (linux-arm).

The output will then be in the "bin\Release\netcoreapp2.0\linux-arm\publish" folder.

For a hello world app, this output might look a bit insane, because along your ~4kb dll you also get ~16MB worth of linux libraries (*.so) as well as 150+ Microsoft dlls weighting in another 15MB.

30 MB seems like a lot for a simple hello world app, however you have to remember that this app is fully self contained and includes everything it needs to run (after all you didn't have to install Mono, any SDKs or other dependencies).

So while on a Windows machine it might seem like you "only have a 4kb dll" you actually end up using the .Net Core framework which either came preinstalled with your Visual Studio or you manually downloaded from here.

And if you look at the website and only download the "run apps" installer for Windows you still end up with a 20MB installer. So 30MB for a fully self-contained linux executable ready to run on a raspi isn't much bigger than what you'd need on Windows.

It's obvious that the hello world app doesn't need all those dependencies and Microsoft is already working on a linker. As a matter of fact they intend to reuse the mono linker as the primary linker for .Net Core.

After copying the deployment folder to your raspi (via WinSCP, remote desktop or share folder) you can run it via "./".

.Net Core on raspi

One can argue which is better (self-contained or framework-dependent) and the documentation lists some pros and cons however for now I won't look further into it as my app is running and I now need to start porting the old code to .Net Core.

tagged as Azure, Home Automation and .Net Core