-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move BackgroundService implementation from StartAsync to the new StartedAsync #88605
Comments
Tagging subscribers to this area: @dotnet/area-extensions-hosting Issue DetailsAs part of #87335, there were discussions that BackgroundService implementation should be moved to Since BackgroundService already has a virtual APInamespace Microsoft.Extensions.Hosting
{
- public abstract class BackgroundService : IDisposable, IHostedService
// Note that IHostedLifecycleService implements IHostedService
+ public abstract class BackgroundService : IDisposable, IHostedLifecycleService
// Keep this, but this method will no longer implement IHostedService.StartAsync
public virtual Task StartAsync(CancellationToken cancellationToken);
+ Task IHostedLifecycleService.StartingAsync(CancellationToken cancellationToken); //noop
+ Task IHostedLifecycleService.StartAsync(CancellationToken cancellationToken); //noop
+ Task IHostedLifecycleService.StartedAsync(CancellationToken cancellationToken); //noop
+ Task IHostedLifecycleService.StoppingAsync(CancellationToken cancellationToken); //noop
// We don't need to explictely implement StopAsync since the existing virtual is fine
+ Task IHostedLifecycleService.StoppedAsync(CancellationToken cancellationToken); //noop
} AlternativeNote that by implementing the new callbacks, it prevents derived classes from also implementing them. Consider the alternative where the new callbacks are all added as virtuals, but this would be breaking since existing overrides of StartAsync will have pre- and post- behavior broken when calling namespace Microsoft.Extensions.Hosting
{
- public abstract class BackgroundService : IDisposable, IHostedService
+ public abstract class BackgroundService : IDisposable, IHostedLifecycleService
+ public virtual Task StartingAsync(CancellationToken cancellationToken);
public virtual Task StartAsync(CancellationToken cancellationToken);
+ public virtual Task StartingAsync(CancellationToken cancellationToken);
+ public virtual Task StoppingAsync(CancellationToken cancellationToken);
public virtual Task StopAsync(CancellationToken cancellationToken);
+ public virtual Task StoppedAsync(CancellationToken cancellationToken);
}
|
This is a behavioral breaking change and makes a lot of assumptions about existing implementations. Please reiterate the rational here for discussion.
|
FWIW, we use BackgroundService to preload data. this works well because this is executed before the server starts. So as @Tratcher said, this would break the behavior und I am not sure what this would do to our application. |
Thanks for the detail - closing this issue. However, based on internal discussion, ASP.NET should consider doing this for https://github.com/dotnet/aspnetcore/blob/main/src/Hosting/Hosting/src/GenericHost/GenericWebHostService.cs |
As part of #87335, there were discussions that BackgroundService implementation should be moved to
StartedAsync()
. This will allow otherIHostedService.StartAsync()
implementations to always run before backgroundservice which prevents order-of-registration issues.Since BackgroundService already has a virtual
Start()
implementingIHostedService.Start()
, we would keep that and explicitly implement the newIHostedLifecycleService
members:API
Alternative
Note that by implementing the new callbacks, it prevents derived classes from also implementing them. Consider the alternative where the new callbacks are all added as virtuals, but this would be breaking since existing overrides of StartAsync will have pre- and post- behavior broken when calling
base.StartAsync()
since the base class implementation (BackgroundService) will no longer have any implementation:The text was updated successfully, but these errors were encountered: