Hosting a service inside ASP.NET Core

A little hidden gem within .Net Core is the ability to run a Hosted Service inside the application. This allows for your service to run in a different thread to the main application, but keeps everything together though the need to host a separate service.

It’s worth taking a look over the Microsoft documentation on how to go about this:

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-3.1&tabs=visual-studio

Setting up is easy as adding a single line to your StartUp method

services.AddHostedService<YourMessageHandler>();