My previous blog post introduced how to make ASP.NET Core 2 web application run on Windows 10 IoT Core. It was default web application created on Visual Studio and published as an executable. But this is not enough for IoT scenarios. When we build web application that runs on IoT board we need this application to do something. Be it displaying sensor data or controlling some hardware. This blog post shows how to make Windows 10 IoT background task talk with web application using WebSocket.
As my way to this solution has been troublesome I list here the main problems I faced so my dear readers have better idea about dead-end streets on this way:
- I was not able to make ASP.NET Core web application run under Windows 10 IoT background service.
- I found no information about when or if it will be supported in near future.
- ASP.NET MVC and ASP.NET Core have different SignalR implementations.
- I was not able to make SignalR client for .NET Core work with SignalR hosted on we application.
I was able to make things work using WebSocket directly. It’s not as nice solution as I had in my mind but it works until things get better.
Making background task and web application talk
I worked out simple and not very well polished solution to make Windows 10 IoT Core background task to communicate with ASP.NET Core web application hosted on Raspberry. I implemented two types of communication on same WebSocket end-point. The following image illustrates what is supported.
To make my sample more informative I also implemented some primitive functionalities to demonstrate how data moves:
- Windows 10 IoT Core background task reports random number to web application after every ten seconds.
- Web application has simple console to send commands to Windows 10 IoT Core background task. Supported commands are:
– opsys: returns operating system information from background task
– machine name: returns machine name where background task runs
Here is the fragment of web application front page when background service runs.
Source code and references
I made my work available at GitHub repository gpeipman/Windows10IoTWebControl.There are background service and web application. Web application must be built and then deployed to Raspberry. Here are some links to help getting started:
- gpeipman/Windows10IoTWebControl (GitHub)
- Running ASP.NET Core 2 applications on Windows 10 IoT Core (Gunnar Peipman)
- ASP.NET Core: Building 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.