The default behavior of the BackgroundService is that StartAsync calls ExecuteAsync, see code. It’s a default, the StartAsync is virtual so you could override it.
Please note that only StartAsync is public and ExecuteAsync
protected (and abstract). So from the outside StartAsync is called
If you create a subclass of BackgroundService, you must implement ExecuteAsync (because it’s abstract). That should do your work. Also you could override StartAsync (as it’s virtual), but that’s only needed for special cases.
So why is there a StartAsync and ExecuteAsync?
You could create a service by implementing IHostedService. That interface has StartAsync and StopAsync.
BackgroundService is an (base) implementation of IHostedService, and could be used for long running tasks. This one defines the abstract ExecuteAsync.
In summary
- When inheriting from
BackgroundService, implementExecuteAsync - When implementing
IHostedService, implementStartAsyncandStopAsync
Read more
- Background tasks with hosted services in ASP.NET Core
- BackgroundService in .NET Core for long running tasks – also other implementations of IHostedService are shown here, like a TimedHostedService