Real-Time Talk: Windows 10 IoT Core Background Tasks and ASP.NET Core Web Apps
Real-Time Talk: Windows 10 IoT Core Background Tasks and ASP.NET Core Web Apps
Learn how to display useful information from your Windows 10 IoT Core application in an ASP.NET Core web app, essential for integrating IoT data into a solution.
Join the DZone community and get the full member experience.
Join For FreeDigi-Key Electronics’ Internet of Things (IoT) Resource Center Inspires the Future: Read More
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.
Problems
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 taskmachine 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.
Digi-Key’s IoT Component Selector is your one-stop-shop for the IoT
Published at DZone with permission of Gunnar Peipman , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}