Deploying .NET Core Apps to the rPi 2

If you’ve been following along with the development of Windows 10 IoT Core, deploying applications to your Raspberry Pi 2 running the OS was focused on UWP. The quickest way to deploy was, of course, through Visual Studio, which offered features to see that goal accomplished successfully.

However, if you tried to drop your rPi 2 app on to a console application, you would be met with a number of complications. Over time, people have indeed managed to do the above, but corners needed to be cut and challenges faced. And really, it was quite a bit of work to run something that was potentially very small.

Bringing the context of this article up to the present day, we find ourselves with a much simpler and up-to-date option. After the appearance of .NET Core 2, which is still in preview, the whole process is now much easier.

If you would navigate to https://github.com/dotnet/cli, you will find the link to download the .NET Core 2.0.0 SDK, which, at the time of this writing, is version 2.0.0-preview2-005889 and shown in Figure 1.

The links to download .NET Core SDK
Figure 1: The links to download .NET Core SDK

Go ahead and download the SDK if you don’t already have it, and then we’ll get started building a console application that we can run on the rPi 2.

Installing Windows 10 IoT Core

Luckily, the folks at Microsoft have done a lot of work in making the installation of Windows 10 IoT Core quite simple. The first bit of software you’ll need on your development system is the Windows IoT Core Dashboard, which is a Windows app, and you can download that from here.

Also, more information on Windows 10 IoT Core can be found through this link.

You’ll also need an SD card to install the operating system on to, which mostly likely would have arrived with your rPi.

If you’re ready to go, you can run the IoT Core Dashboard, which looks something like what we see in Figure 2.

The Windows IoT Core Dashboard running
Figure 2: The Windows IoT Core Dashboard running

Now, Microsoft presents us with a very handy getting started guide, which I suggest following; but, for our needs, we only need to go as far as Step 3 because we won’t be deploying a UWP app to your Pi. Do note that when you run IoT Core for the first time, it does take sometime to start up, and I might suggest connecting a monitor so you can observe the progress. Also, please leave your IoT Core Dashboard open through the remainder of this article; there are several features we’ll be using.

Building the .NET Core Application

Let’s come away from the Pi for a moment, and quickly put together the console application we’ll run from the Pi. I’m using Visual Studio 2017, which is required for the version of .NET Core we’ll be using.

We’ll also be creating a self-contained console application, which removes the need to install the .NET Core runtime on the Pi itself. Everything our application needs to run will be packed with the application when we publish it.

Now, go ahead and create a .NET Core application. You can use Visual Studio’s project templates if you want, but, if you, do we’ll need to make some changes to the csproj file because that project make targets an earlier release build of the framework.

Once your project has been created, right-click the project name and click edit *.csproj (see Figure 3).

Editing the csproj file
Figure 3: Editing the csproj file

If your project targets an older .NET Core framework, you may have a *.csproj file that looks like this…

<Project Sdk="Microsoft.NET.Sdk">

   <PropertyGroup>
      <OutputType>Exe</OutputType>
      <TargetFramework>netcoreapp1.1</TargetFramework>
   </PropertyGroup>

</Project>

Let’s change that XML to look more like this…

<Project Sdk="Microsoft.NET.Sdk">

   <PropertyGroup>
      <OutputType>Exe</OutputType>
      <TargetFramework>
         netcoreapp2.0
      </TargetFramework&gt
      <RuntimeFrameworkVersion>
         2.0.0-beta-001745-00
      </RuntimeFrameworkVersion>
      <RuntimeIdentifiers>
         win8-arm
      </RuntimeIdentifiers>
   </PropertyGroup>

</Project>

There’s two more lines added (they’re highlighted in yellow), so let’s examine them first. The RuntimeFrameworkVersion element is used to focus on what version of the framework we’ll be using. This can greatly reduce the amount of code packed with our application, and ultimately gives us a better start-up time.

The RuntimeIdentifiers element is one of the more important ones for our use. Because we will be running this application on the Pi, we need a way of telling the compiler we need to build for the ARM chipset; it is not x86-based, as we are more used to on our dev systems. This element does this for us, and, for our needs, win8-arm is the choice identifier to use here.

Because this is a basic console application, let’s have it output a simple bit of text that will give us a visual indicator it worked when we run it. I’ve edited the code in our program.cs file to reflect what we have here…

class Program
{
   static void Main(string[] args)
   {
      Console.WriteLine("This message came from
         the other side, the side of the Pi!");
   }
}

Now, let’s build our application.

Open the command window, or powershell window, at the root of your project and type…

dotnet restore
dotnet build
dotnet publish -c Release -r win8-arm

If everything was successful, you should find in the bin/Release folder of your project another folder named netcoreapp2.0, which in turn has a folder named win8-arm, shown in Figure 4.

The result of the netcore publish
Figure 4: The result of the netcore publish

Now, the publish folder—underlined in Figure 4—is the folder we need to bring our attention to. It is everything in that folder which we’ll be copying to our Pi. Everything else in the win8-arm folder can be ignored.

So, let’s copy the published project to our Pi. Using the Windows IoT Core Dashboard we left open earlier, go to My Devices, and right-click your device shown in the list, just as we are shown in Figure 5, and select Open network share.

Opening a network share from the IoT Daskboard
Figure 5: Opening a network share from the IoT Daskboard

You will be prompted to enter your credentials, where, once done, you’ll see an Explorer window opened at the root of you rPi. From here, let’s create a folder named apps, then inside that folder another folder named something like testapp. This test app folder is where we’ll copy our application (see Figure 6).

The created apps folder at the root of our rPi
Figure 6: The created apps folder at the root of our rPi

All that remains now is to copy our published application to our apps/testapp folder. Just do a straight copy and paste. And, make sure to do so just on those files found in the publish folder. Then, once you’re done, go back to the IoT Dashboard, my devices, right-click your Pi, and select Launch PowerShell.

Again, you’ll be asked to enter your admin credentials; but, once done, we can use PowerShell to navigate to the folder we named testapp; and type the following command…

./IotTestApp.exe

If you named your project something other than what was used in this article, please go ahead and amend the naming as needed. If all worked well, you should see a result that looks something like what’s shown in Figure 7.

The PowerShell window used to run our console application
Figure 7: The PowerShell window used to run our console application

As we can see, and running from Windows 10 IoT Core on the Raspberry Pi 2, we have the output which reads ‘This message came from the other side, the side of the Pi!’ just as was specified in the program.cs of our application.

Conclusion

In the earlier days of Windows 10 IoT Core, the only applications we could run were UWP. Yes, you could run other stuff, but it often required quite a bit of work to do so.

Now, you can run non-UWP applications quite easily, and I would guess increasingly easier as .NET Core 2.0 develops further. If you have any questions about this article, you can find me on Twitter @GLanata, and have fun with your Pi!

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read