Introducing BackgroundService
The BackgroundService
class was introduced in ASP.NET Core 3.0 and is basically an abstract class that implements the IHostedService
interface. It also provides an abstract method, called ExecuteAsync()
, which returns a Task
.
If you want to reuse the hosted service from the last section, the code will need to be rewritten. Follow these steps to learn how:
- First, write the class skeleton that retrieves
ILogger
viaDependencyInjection
:namespace HostedServiceSample; public class SampleBackgroundService : BackgroundService { private readonly ILogger<SampleHostedService> logger; // inject a logger public SampleBackgroundService( ILogger<SampleHostedService> logger) { this.logger = logger; &...