I like developing in C# and using Azure, and this is a blog about what technologies I'm working on at the moment.

Running a .NET Core 2 app on Raspbian Jessie, and deploying to the Pi with Cake

I’ve managed to get .NET Core apps running on Windows 10 IoT Core, and on Ubuntu 16.04 (and also Ubuntu MATE), but until recently I’d never tried with Raspbian. I’ve read a few posts from people saying that they couldn’t get it to work, and a couple of nights ago I decided to bite the bullet and give it a go.

There’s a good post here on how to get the .NET Core runtime on a Raspberry Pi running Raspbian, and as always, there are a few tricks to getting things running from scratch. To augment this, I’ve created a “Hello World” application template for .NET Core Raspberry Pi on NuGet which I think will make things easier for the community.

At a very high level, the steps to getting a .NET Core 2 app on Raspbian are:

Install Raspbian onto an SD card and insert into your Raspberry Pi 3.

Set up SSH and test that you can log into your Raspberry Pi from your development machine.

Install .NET Core 2 onto the Raspberry Pi.

On your development machine, install the Raspberry Pi C# template for dotnet core.

Create a new console application using this template.

Deploy this application to your Pi running Raspbian using Cake.

I’ll run through each of these steps below.

Install Raspbian onto an SD card and insert into your Raspberry Pi 3

There are already great explanations of how to install the Raspbian OS onto a Raspberry Pi 3 – many people who have a Pi know how to do this already and I don’t really want to just repeat a well understood process here – so I’ve just put some useful links below:

Set up SSH and test that you can log into your Raspberry Pi from your development machine

Once you’ve set up Raspbian and booted your Pi to the desktop, you’ll need to allow SSH connections. These aren’t enabled by default but it’s very easy to configure this.

First open the main Raspberry Pi menu on your desktop as shown in the image below, and open the Preferences sub-menu to reveal the “Raspberry Pi Configuration” option.

Open the “Raspberry Pi Configuration” option screen, and click on the “Interfaces” tab. There’s lots of useful settings here, but the one we want to enable is SSH – click on the “Enabled” radio button as shown below, and then click on OK. SSH is now enabled on your Pi.

You’ll need to find the IP address of your Raspberry Pi – I think the easiest way is to open a terminal on your Pi and type:

hostname -I

This tells me that my Pi has the IP address 192.168.1.111.

Now we need to check you can log in from your development machine. I personally find it’s easiest to use PuTTY to do this. I’ve blogged about installing PuTTY before so I won’t repeat it all here – but a few tips are:

This bit is really important: When you install PuTTY using the installer, it ships with a couple of other programs – pscp.exe and plink.exe, which live in the same directory as putty.exe. You’ll need both of these to deploy the code to the Pi. Pscp.exe allows you to copy files from Windows to Linux, and plink allows you to remotely change permissions on the files you deploy.

It also makes life easier to add the putty installation directory to your machine’s path. If Cake doesn’t know where pscp and plink are on your machine, then you’ll probably see and error about unknown executables during deployment.

So open PuTTY, enter the Pi’s IP address and select the “Connection Type” to be SSH, as shown below:

When you click Open, a command prompt should open where you can type the username and password for the Pi 3.

The default username and password combo for Raspbian is “pi” and “raspberry“, and you should change the default password as soon as possible.

Install .NET Core 2 onto the Raspberry Pi

There’s a straightforward set of commands that you can run through PuTTY to install .NET Core 2 onto your Pi running Raspbian – I’ve written them below:

I’ve created a folder on the Pi to deploy my application to, using the command below at the PuTTY SSH prompt at my home directory (/home/pi/).

mkdir -p DotNetConsoleApps/RaspbianTest

Deploy this application to your Pi running Raspbian using Cake

Once I’ve replaced the placeholders in my Cake file, the only thing left to do is run the build.ps1 file from a PowerShell prompt.

To test this, go back to the PuTTY SSH prompt and navigate to your home directory and run:

./DotNetConsoleApps/RaspbianTest/HelloRaspbian

And you’ll get a text output saying “Hello Internet of Things!”

Wrapping up

I hope this post is useful to anyone trying to get a C# console application running on Raspbian. I think Raspbian is the default OS for Raspberry Pi users, so this should open up many development opportunities. My Raspberry Pi template makes creating the default console application easier, and Cake is a brilliant way to orchestrate the deployment process (rather than dragging and dropping files using tools like WinSCP, and having to change file permission manually). I’ll be blogging more on the future on deploying IoT applications to this platform.

I’ve written a few posts now about how to deploy C# Raspberry Pi applications to Windows 10 IoT Core, Ubuntu, and Raspbian (all using Cake as the orchestration tool) – next time I’ll write about how to use Cake to automatically build a UWP AppxBundle and deploy that AppxBundle to Windows 10 IoT Core.

About me: I regularly post about .NET – if you’re interested, please follow me on Twitter, or have a look at my previous posts here. Thanks!

It’s scary how timely this was! I just today needed to get rolling with a new .net core app on the RPi 3, got my Pi in the mail just now (7/24/2017) and here’s your article. Thanks for the great kickstart!!

I did hit a speedbump at the end: I could not get the simple hello world app to build and produce an executable. the .dll came out, but the Cake deploy step complained that there was no executable. I ensured I had .Net Core 2 preview 2 installed, but still no dice. I’ve got stock visual studio 2017, but will shortly have 2017 v15.3 installed and try that. Any hints/suggestions on getting my build done besides the above? 🙂 thanks again much for lighting the way.

I’ve only used PowerShell and VSCode, so there shouldn’t be a dependency on VS2017. Do you mind if I drop you an email to correspond, maybe we can diagnose the issue together – I’ll post the resolution as a reply here once we work it out?

Thanks for the article. But I have problem with Deploy, after execute script I got error:
An error occurred when executing task ‘Deploy’.
Error: Pscp: Could not locate executable.
I have also tried connect to raspbbery by Putty and it works fine.
I use rapsbian system.
Do you have any idea what is wrong?

Hey – sounds like you don’t have PSCP on your machine, or it’s not on the path. Do you have a file called “pscp.exe” in your “C:\Program Files\PuTTY” directory? And if you type pscp at a command prompt (not in that folder) do you get a message about “PuTTY Secure Copy client”?

I’m not sure I got it right. When I put “dotnet” in the console I get a Segmentation fault. I followed your steps all the way until I hit the “dotnet –info” step. Any ideas? Sounds like either dotnet itself has a bug or more likely I missed a pre-req. But I followed your scripts to the letter.