Introducing IHostedService
Hosted services have been a thing since ASP.NET Core 2.0 and can be used to run tasks asynchronously in the background of your application. They can be used to fetch data periodically, do some calculations in the background, or to do some cleanups. You can also use them to send preconfigured emails – or whatever you need to do in the background.
Hosted services are basically simple classes that implement the IHostedService interface. You call them with the following code:
public class SampleHostedService : IHostedService
{
public Task StartAsync(CancellationToken
cancellationToken)
{
}
public Task StopAsync(CancellationToken
cancellationToken)
{
}
}
IHostedService needs to implement a StartAsync() method and a StopAsync() method. The...