Dotnet Core
Jared Rhodes  

Creating an ASP.NET Core application for Raspberry Pi

As a part of the Wren Hyperion solution, an ASP.NET Core application will run on an ARM based Linux OS (we are building a POC for the Raspberry Pi and Raspian).  Here are the steps on how you can get started with ASP.NET Core on Raspian:

  • Setup a Raspberry Pi with Raspian
  • Install .NET Core
  • Create your ASP.NET Core solution
  • Publish to the Raspberry Pi
  • Install .NET Core to the Raspberry Pi
  • Run your application on the Raspberry Pi

Setup a Raspberry Pi with Raspian

Follow the guides on the Raspberry Pi website to install Raspian on your Pi.

Install .NET Core

  1. Download and Install

    To start building .NET apps you just need to download and install the .NET SDK (Software Development Kit) for Windows.

    Download .NET SDK

    A video frame showing a Windows command prompt. Select this image to start playing the video.

    Video: Creating a Simple .NET Application on Windows

  2. Get an editor

    Visual Studio is a fully-featured integrated development environment (IDE) for developing .NET apps on Windows.

    Download Visual Studio

    Select the .NET Core cross-platform development workload during installation:

    A screenshot of the Visual Studio Installer. The Workload screen is shown, and '.NET Core cross-platform development' is selected.

  3. More info

    While you wait for Visual Studio to install, you can keep learning with the .NET Quick Starts. In the first Quick Start you’ll learn about collections.

    Quick start: Collections

    Once Visual Studio is installed, come back and build your first .NET app using Visual Studio.

Create your ASP.NET Core Solution

  1. Create a new .NET Core project.

    On macOS and Linux, open a terminal window. On Windows, open a command prompt.

    dotnet new razor -o aspnetcoreapp
  2. Run the app.Use the following commands to run the app:
    cd aspnetcoreapp
    dotnet run
    
  3. Browse to https://localhost:5000
  4. Open Pages/About.cshtml and modify the page to display the message “Hello, world! The time on the server is @DateTime.Now “:
    @page
    @model AboutModel
    @{
        ViewData["Title"] = "About";
    }
    <h2>@ViewData["Title"]</h2>
    <h3>@Model.Message</h3>
    
    <p>Hello, world! The time on the server is @DateTime.Now</p>
    
  5. Browse to https://localhost:5000/About and verify the changes.

Publish to the Raspberry Pi

On macOS and Linux, open a terminal window. On Windows, open a command prompt and run:
dotnet clean .
dotnet restore .
dotnet build .
This will rebuild the solution. Once that is complete run the following command:
dotnet publish . -r linux-arm
This will generate all the files needed for running the solution on the Raspberry Pi. After this command completes generating the needed files, the files need to be deployed to the Pi. Run the following command in Powershell from the publish folder:
& pscp.exe -r .\bin\Debug\netcoreapp2.0\ubuntu.16.04-arm\publish\* ${username}@${ip}:${destination}
Where username is the username for the Pi (default “pi”), ip is the ip address of the Pi, and destination is the folder the files will be published to.

Install .NET Core to the Raspberry Pi

This section is sourced from Dave the Engineer’s post on the Microsoft blog website.

The following commands need to be run on the Raspberry Pi whilst connected over an SSH session or via a terminal in the PIXEL desktop environment.

  • Run sudo apt-get install curl libunwind8 gettext. This will use the apt-get package manager to install three prerequiste packages.
  • Run curl -sSL -o dotnet.tar.gz https://dotnetcli.blob.core.windows.net/dotnet/Runtime/release/2.0.0/dotnet-runtime-latest-linux-arm.tar.gz to download the latest .NET Core Runtime for ARM32. This is refereed to as armhf on the Daily Builds page.
  • Run sudo mkdir -p /opt/dotnet && sudo tar zxf dotnet.tar.gz -C /opt/dotnet to create a destination folder and extract the downloaded package into it.
  • Run sudo ln -s /opt/dotnet/dotnet /usr/local/bin` to set up a symbolic link…a shortcut to you Windows folks 😉 to the dotnet executable.
  • Test the installation by typing dotnet –help.

  • Try to create a new .NET Core project by typing dotnet new console. Note this will prompt you to install the .NET Core SDK however this link won’t work for Raspian on ARM32. This is expected behaviour.

Run your application on the Raspberry Pi

Finally to run your application on the Raspberry Pi, navigate to the folder where the application was published and run the following command:

dotnet run

You can then navigate to the website hosted by the Raspberry Pi by navigating to https://{your IP address here}:5000.

18 thoughts on “Creating an ASP.NET Core application for Raspberry Pi

  1. Mario Rost

    Hello Jared Rhodes,
    I followed your detailed instruction and got finally the same message with the prompt to install .NET Core SDK. Unfortunately, I got the same message by trying to run my asp.net – webapplication. I published it in the same way you described. Do you know if i have to change some logs in files? Or maybe placing the published folder into the root system, but the functionalities are only installed on the pi, aren´t they? May it be the current version of my debian on the Raspberry in relation to the compiled version above? The website is unproblematically working on my windows system.
    So far I devised two aspects:
    1. Wrong installed packages on the Raspberry. ~ installed everything again (deleted the symbolic link etc.)
    2. Wrong created application, wrong publishing

    I wann create a webapplication (website) with functionalities (FTP, SSH, RMD, display of files). I decided to use an asp.net-webapplication to realise it. So far I need to reach the milestone of the ability to have a running website. I hope you can help me. I will still working on this probematic and will upload the solution of it. I think I am not the only one.

    Regards
    Mario

    1. Jared Rhodes

      What is the error message?

  2. uckpa

    pi@raspberrypi:/opt/dotnet $ dotnet –help

    Usage: dotnet [host-options] [path-to-application]

    path-to-application:
    The path to an application .dll file to execute.

    host-options:
    –additionalprobingpath Path containing probing policy and assemblies to probe for
    –depsfile Path to .deps.json file
    –runtimeconfig Path to .runtimeconfig.json file
    –fx-version Version of the installed Shared Framework to use to run the application.
    –roll-forward-on-no-candidate-fx Roll forward on no candidate shared framework is enabled
    –additional-deps Path to additonal deps.json file

    Common Options:
    -h|–help Displays this help.
    –info Displays the host information

    pi@raspberrypi:/opt/dotnet $
    pi@raspberrypi:~/mytest $ dotnet new console
    Did you mean to run dotnet SDK commands? Please install dotnet SDK from:
    http://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409

    pi@raspberrypi:~/mytest $

    1. Jared Rhodes

      it looks like the symbolic link isn’t working. Does running “sudo ln -s /opt/dotnet/dotnet /usr/local/bin” and restarting fix it?

  3. Marcel Benders

    Hey Jared,

    thx for the Blogpost. But I’ve got an error message when i try to execute the ./aspnetcoreapp:

    Error:
    An assembly specified in the application dependencies manifest (aspnetcoreapp.deps.json) was not found:
    package: ‘Microsoft.ApplicationInsights.AspNetCore’, version: ‘2.1.1’
    path: ‘lib/netstandard1.6/Microsoft.ApplicationInsights.AspNetCore.dll’

    If I try to execute the app with “dotnet run” my raspi says:

    Did you mean to run dotnet SDK commands? Please install dotnet SDK from:
    http://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409

    Any idea?

    Regard

    Marcel

    1. Jared Rhodes

      Could this be an issue of user permissions to the dll? Try running your commands with sudo.

      1. Marcel Benders

        Hey Jared, thx for the quick response. Nope, that solved not the problem 🙁

        1. Jared Rhodes

          Can you verify that the Microsoft.ApplicationInsights.AspNetCore.dll is in the publish directory?

  4. Marcel Benders

    Jep:

    pi@DotNetPi:~/source/ubuntu.16.04-arm/publish $ ls Microsoft.ApplicationInsights.*
    Microsoft.ApplicationInsights.AspNetCore.dll Microsoft.ApplicationInsights.dll
    pi@DotNetPi:~/source/ubuntu.16.04-arm/publish $

    1. Jared Rhodes

      At this point, I think you should reach out to the ASP.NET team on GitHub. I am not going to be as much help. https://github.com/aspnet/Home

      1. Marcel Benders

        Okay. I found the error. I have to start the program from the publish folder. Thank you for your help.

  5. Rrrr

    absolutely wrong article, this approach does not work

    1. Jared Rhodes

      What about it doesn’t work. We use it for provisioning RPi3s right now

      1. Thou

        To publish my project I copy publish file into RPi and run command “dotnet NameOfProject.dll”. Im curious, why u use “dotnet publish . -r ubuntu.16.04-arm” instead of “dotnet publish -r linux-arm”.

        1. Jared Rhodes

          I should update it to use -r linux-arm

    2. Marcel Benders

      I use it too and I’m very happy 😀

  6. Stas Kiyan

    For me dotnet run not worked, because on raspberry pi we only have .net core runtime, but not sdk.
    I used dotnet instead.

  7. […] by /u/toothkiller [link] […]

Leave A Comment