Recently I discovered that it’s possible to write .NET Core 2.0 applications which target the ARM processor architecture. There’s already some good instructions  on how to get started with this, and I wanted to write about my own experiences of the end to end process – creating the application, deploying it, and then running it on a Raspberry Pi.

Setting up my development machine

I’ve started with a fresh Windows 10 x64 virtual machine, and installed the latest pre-release version of the .NET Core 2.0 SDK/CLI tools from here. (This is the only version of .NET Core that I installed, which is probably different from most developer’s environment).

This install gives me the .NET runtime version of: 2.0.0-beta-001745-00.

It also gave me the .NET SDK version of: 2.0.0-preview1-005418.

You can get the runtime version by typing dotnet at a Powershell prompt, and you can get the SDK/CLI version by typing dotnet –version. Julie Lerman has a post on this here.

At a command prompt, navigate to the directory that you want to create the project in and type:

dotnet new console -n coreiot

This will create a new folder with the name coreiot (because it’s an internet of things project written in .NET Core), and inside this project there are three files:

  • coreiot.csproj
  • NuGet.config
  • Program.cs

We need to make some changes to coreiot.csproj. By default on my machine (with only the .NET Core 2.0 pre-release SDK and runtime installed), it has the text below:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <RuntimeFrameworkVersion>2.0.0-beta-001745-00</RuntimeFrameworkVersion>
  </PropertyGroup>
</Project>

You might have different runtime versions on your machine, and the default runtime version might be different from the one above – but for this to work, it can’t be lower than 2.0.0-beta-001620-00, and you need to have at least this version installed.

We just need to add a single line to this file to name the possible runtime identifiers. This allows us to define the platform to target when we’re publishing the application.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <RuntimeFrameworkVersion>2.0.0-beta-001745-00</RuntimeFrameworkVersion>
    <RuntimeIdentifiers>win8-arm;ubuntu.14.04-arm;ubuntu.16.04-arm</RuntimeIdentifiers>
  </PropertyGroup>
</Project>

At this point, you can change directory into the coreiot directory, and run:

dotnet restore

The first time you run this, it might take a very long time to restore all the packages. After this, the next command depends on the kind of OS that you have installed on your Raspberry Pi.

If you’re running Windows 10 IoT Core (as I am), then you can run:

dotnet publish -r win8-arm

Alternatively, if you’re running Ubuntu 16.04, you can run:

dotnet publish -r ubuntu.16.04-arm

These commands will generate all the files necessary to run the application on your IoT device. For Windows 10 IoT Core, I have to navigate from the root coreiot directory all the way through to the directory below.

\coreiot\bin\Debug\netcoreapp2.0\win8-arm\publish

The files in the publish directory are all necessary to run the Core IoT console application.

screenshot.1489956305

On my Windows development machine, there are 167 files in the publish directory and these total to a size of about 37MBytes.

Deploying my .NET Core App to Windows 10 IoT Core

My Raspberry Pi 3 is running Windows 10 IoT Core, version 15051 – when I start it up, it always has the IP address 192.168.1.111 – your machine might be different. I find this IP address from the Microsoft IoT Dashboard (shown below).

screenshot.1489955974

It’s really pretty straightforward to share files from my development machine to my Raspberry Pi (it’s described in lots of detail here). I browse to \\192.168.1.111\c$ in a standard Windows Explorer window, and this shows the file and directory structure of the Raspberry Pi.

A super easy way to do this is just right-click on the device in the IoT Dashboard and select “Open Network Share”. This will open a Windows Explorer for the C drive of the IoT device.

screenshot.1489958606

I created a new folder called CoreApplications, and within this I created a folder called coreiot to match the name of my application. Below I’ve shown the coreiot folder and the publish folder side by side.

screenshot.1489956570

I select all of the files in the publish directory on my development machine and dragged them to the coreiot directory on my Raspberry Pi, shown below.

screenshot.1489956712

How to run the app on Windows 10 IoT Core

There’s a few different ways to connect to the Raspberry Pi, either using PowerShell or SSH with PuTTY, but I think the easiest way is to use the IoT Dashboard again, and right click on the Raspberry Pi device and select “Launch PowerShell”, as shown in the image below.

screenshot.1489958874

This will open a PowerShell prompt and a dialog challenging you for your administrative password.

screenshot.1489959150

It takes a few seconds to connect (maybe even up to 30 seconds) – until then it just shows an empty blue screen – but eventually the prompt will return, as shown below.

screenshot.1489959232

At this point, I can navigate to the directory where I copied my console application (C:\CoreApplications\coreiot) and then I can run the executable using:

./coreiot.exe

This outputs “Hello World!”, as shown below.

screenshot.1489956903

Summary

This very simple application only works with .NET Core v2.0, which is presently not the released version. The application doesn’t do much (of course, hello world applications usually don’t), but proves that we can run platform independent executable applications on the Windows 10 IoT Core devices with code that can be re-compiled and deployed to an Ubuntu device.