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