Step by step: Running ASP.NET Core on Raspberry Pi

After reading .NET Core on Raspberry Pi and successfully running a console application on Windows 10 IoT Core on my Raspberry Pi 3 I decided to write: Step by step: Running ASP.NET Core on Raspberry Pi.

First be aware of the following prerequisites:

  • Windows 10 IoT Core I’m running Insider Preview v.10.0.15058.0
  • .NET Core 2.0 SDK

Now let’s start:

1. Create a folder for your new project


Open a command prompt an run

1mkdir aspnet.on.rpi
2cd aspnet.on.rpi
3code .

2. Create a global.json file


To specify the correct sdk create a global.json with the following contents

1{
2    "sdk": {
3        "version": "2.0.0-preview1-005448"
4    }
5}

3. Create the ASP.NET Core project


Create the ASP.NET Core project with the following command:

1dotnet new mvc

4. Modify the project file


Modify the aspnet.on.rpi.csproj to add the correct OutputType, TargetFramework, RuntimeFrameworkVersionand RuntimeIdentifiers

 1<Project Sdk="Microsoft.NET.Sdk.Web">
 2
 3  <PropertyGroup>
 4    <OutputType>Exe</OutputType>
 5    <TargetFramework>netcoreapp2.0</TargetFramework>
 6    <RuntimeFrameworkVersion>2.0.0-beta-001776-00</RuntimeFrameworkVersion>
 7    <RuntimeIdentifiers>win8-arm</RuntimeIdentifiers>
 8  </PropertyGroup>
 9
10  <ItemGroup>
11    <PackageReference Include="Microsoft.AspNetCore" Version="1.0.3" />
12    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.0.2" />
13    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.0.1" />
14    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.0.1" />
15    <PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.0.1" />
16    <PackageReference Include="Libuv" Version="1.10.0-preview1-22036" />
17  </ItemGroup>
18</Project>

5. Add a Nuget.config file and restore packages

Create a Nuget.config file with the following contents:

1<?xml version="1.0" encoding="utf-8"?>
2<configuration>
3  <packageSources>
4    <add key="dotnet-core" value="https://dotnet.myget.org/F/dotnet-core/api/v3/index.json" />
5  </packageSources>
6</configuration>

Now restore the packages:

1dotnet restore

6. Modify Program.cs


Replace the contents of the Program.cs file with the following code

 1using System;
 2using System.Collections.Generic;
 3using System.IO;
 4using System.Linq;
 5using System.Threading.Tasks;
 6using Microsoft.AspNetCore.Hosting;
 7
 8namespace aspnet.on.rpi
 9{
10    public class Program
11    {
12        public static void Main(string[] args)
13        {
14            var host = new WebHostBuilder()
15                .UseKestrel()
16                .UseUrls("http://*:5000") // Using * to bind to all network interfaces
17                .UseContentRoot(Directory.GetCurrentDirectory())
18                .UseIISIntegration()
19                .UseStartup<Startup>()
20                .Build();
21
22            host.Run();
23        }
24    }
25}

7. Publish the application


Publish the application with the following commands

1dotnet publish -r win8-arm

We need to publish for win7-arm as a workaround to copy the correct libuv.dll in the next step.

  1. Copy libuv.dll

Copy libuv.dll from \aspnet.on.rpi\bin\Debug\netcoreapp2.0\win7-arm\publish to \aspnet.on.rpi\bin\Debug\netcoreapp2.0\win8-arm\publish

This step is no longer needed cause we added libuv as a dependency in the csproj file

9. Copy the files to your Raspberry


Connect to Raspberry using PowerShell, start the ftp server and open port 5000 on the Raspberry

1Enter-PSSession -ComputerName <Raspberry IP> -Credential <Raspberry IP>\Administrator
2start C:\Windows\System32\ftpd.exe
3netsh advfirewall firewall add rule name="Open Port 5000" dir=in action=allow protocol=TCP localport=5000

Open the File Explorer ftp://<TARGET_DEVICE> and copy the contents of \aspnet.on.rpi\bin\Debug\netcoreapp2.0\win8-arm\publish to a folder on your Raspberry (i.e. c:\publish).

10. Run the application


Connect to Raspberry using PowerShell and run

1cd c:\publish\
2.\aspnet.on.rpi.exe

You should be good to go and be able to browse on port 5000 of you RPi.

Get the code here: https://github.com/cmendible/dotnetcore.samples/tree/main/aspnet.on.rpi

Hope it helps!


Twilio with .NET Core
Raspberry Pi: Run ASP.NET Core on Startup
comments powered by Disqus