My previous blog post introduced how to make an ASP.NET Core 2 web application run on Windows 10 IoT Core. It was a default web application created on Visual Studio and published as an executable. But this is not enough for IoT scenarios. When we build a web application that runs on an IoT board, we need this application to do something — displaying sensor data or controlling some hardware, etc. This blog post shows how to make a Windows 10 IoT background task talk with a web application using WebSockets.
As my path to this solution has been troublesome, I am listing here the main problems I faced so my dear readers have a better idea of dead-end streets along the way:
- I was not able to make the ASP.NET Core web application run under a Windows 10 IoT background service.
- I found no information about when or if it will be supported in the near future.
- ASP.NET MVC and ASP.NET Core have different SignalR implementations.
- I was not able to make a SignalR client for .NET Core work with SignalR hosted on a web application.
I was able to make things work by directly using a WebSocket. It’s not as nice a solution as I had in my mind, but it works until things get better.
Making the Background Task and Web Application Talk
I worked out simple and not-very-well polished solution to make a Windows 10 IoT Core background task communicate with an ASP.NET Core web application hosted on a Raspberry Pi. I implemented two types of communication on the same WebSocket endpoint. The following image illustrates what is supported.
To make my sample more informative, I also implemented some primitive functionalities to demonstrate how data moves.
- The Windows 10 IoT Core background task reports random numbers to the web application every 10 seconds.
- The web application has a simple console to send commands to the Windows 10 IoT Core background task. Supported commands are:
opsys: returns operating system information from the background task
machine name: returns the machine name where the background task runs
Here is a fragment of the web application's front page when the background service runs.
Source Code and References
I made my work available at my GitHub repository. There are background service and web applications. The web application must be built and then deployed to the Raspberry Pi. Here are some links to help get started:
- gpeipman/Windows10IoTWebControl (GitHub)
- Running ASP.NET Core 2 applications on Windows 10 IoT Core (Gunnar Peipman)
- ASP.NET Core: Building a chat room using WebSocket (Gunnar Peipman)
- Communicating with localhost (loopback) (Windows IoT/Microsoft Docs)
Please feel free to give me feedback about better solutions available.