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
-
Download and Install
To start building .NET apps you just need to download and install the .NET SDK (Software Development Kit) for Windows.
Video: Creating a Simple .NET Application on Windows
-
Get an editor
Visual Studio is a fully-featured integrated development environment (IDE) for developing .NET apps on Windows.
Select the
.NET Core cross-platform development
workload during installation: -
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.
Once Visual Studio is installed, come back and build your first .NET app using Visual Studio.
Create your ASP.NET Core Solution
-
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
- Run the app.Use the following commands to run the app:
cd aspnetcoreapp dotnet run
- Browse to https://localhost:5000
- 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>
-
Browse to https://localhost:5000/About and verify the changes.
Publish to the Raspberry Pi
dotnet clean .
dotnet restore .
dotnet build .
dotnet publish . -r linux-arm
& pscp.exe -r .\bin\Debug\netcoreapp2.0\ubuntu.16.04-arm\publish\* ${username}@${ip}:${destination}
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.
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
Jared Rhodes
What is the error message?
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 $
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?
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
Jared Rhodes
Could this be an issue of user permissions to the dll? Try running your commands with sudo.
Marcel Benders
Hey Jared, thx for the quick response. Nope, that solved not the problem 🙁
Jared Rhodes
Can you verify that the Microsoft.ApplicationInsights.AspNetCore.dll is in the publish directory?
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 $
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
Marcel Benders
Okay. I found the error. I have to start the program from the publish folder. Thank you for your help.
Rrrr
absolutely wrong article, this approach does not work
Jared Rhodes
What about it doesn’t work. We use it for provisioning RPi3s right now
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”.
Jared Rhodes
I should update it to use -r linux-arm
Marcel Benders
I use it too and I’m very happy 😀
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.
Creating an ASP.NET Core application for Raspberry Pi - How to Code .NET
[…] by /u/toothkiller [link] […]