Blazor WASM .NET 7 running on a Raspberry PI 4

blazorwasm .NET 7 on a Raspberry PI

Ok, this is exciting.  The .NET framework has come a long way in the last few years and what MS has done with Blazor is incredible.  Sure, this tech stack has been around a little while, but its exciting for me because its the first time I've married it all together.

I recently worked on a application to let users communicate the seating availability of a theater and we wanted to do it in real-time. This sounded like a job for Websockets and what better way to do it then using a Blazor WASM hosted application??

Here is the process.

1. The Raspberry PI

Start with a functional RPI. I decided to use a Raspberry PI 4 kit I picked up from canakit a few years ago.  It had 4GB of RAM which is plenty to host this type of application. I wanted to run it off USB so I first had to enable that boot option with help from here: https://linuxhint.com/boot-raspberry-pi-from-usb/

Then I just took the standard image using the RPI imager.  Depending one whether or not you pre-configure SSH and Wifi, you may have additional setup before you get into the Rasbian GUI.  Once you are in, open a Terminal or connect of SSH from your development machine.

 

2. Getting dotnet setup

This Microsoft article gets you part of the way there: https://learn.microsoft.com/en-us/dotnet/iot/deployment.  The extraction of the .NET 7 framework took almost 20 minutes for me so be patient, it should work. Once you have dotenet setup, you're ready to deploy your application.
 

3. Publish the application

I already had a working application on my develpoment machine so I just needed to get it moved over and running.
 
During step 4 of that Microsoft article, I had trouble getting the scp command going.  That's when I turned to WinSCP which makes everything a lot easier.
 
Not being a linux person, I wasn't sure where to put the app. I came across an article which suggesting running it as a Service so I put it under /svr/APPLICATION_NAME and followed the rest of the steps
 
Once its moved over, you should be able to navigate to the publish folder and run something like:
dotnet YourAppName.dll

Which is good, but if you want to reach your running application outside of localhost, you may need to provide another URL like this wildcard version:

dotnet YourAppName.dll --urls "http://*:5000"
 

4. Register the application as a service

I came across an older article on this registration here: https://swimburger.net/blog/dotnet/how-to-run-a-dotnet-core-console-app-as-a-service-using-systemd-on-linux
 
One of the problems with using a service or a crontab entry to execute your dll is that if you don't do it from the published directory, the application will start but every request results in a 404.  This is because you need to set the working directory before calling the "dotnet YourAppName.dll" command.
 

For me, that looked like this:

WorkingDirectory=/srv/MyAppFolder/
ExecStart=...rest of command...
 
If you add the service file to your solution, make sure you include it in the publish with an item group:
 
Item Group service file
 
 
 
 
This article also includes steps for using the newer .NET Core worker template but I didn't take it that far.
 

Conclusion

 
Running Blazor WASM over a Raspberry PI is an affordable and low-barrier alternative to cloud hosting.  If you want to see the code that worked for me, check out this github repo: https://github.com/shanabus/tcp-tcp