ASP.NET Core Background Service

Background tasks are often delegated to worker projects or external libraries to be handled, but ASP.NET Core has something like this inbuilt.
The actual interface is IHostedService and is registered in startup.cs like:


That’s cool, but there’s also a built in abstract class to handle a lot of the plumbing as well called BackgroundService.
Implementing this class requires an override for ExecuteAsync(CancellationToken stoppingToken) and that’s it. I’m looking to use this for doing simple repeating tasks without extra libraries.

Tags: aspnet