-
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
BackgroundService that doesn't call base.StartAsync leads to NullReferenceException - .NET 6 breaking change #60131
Comments
Tagging subscribers to this area: @eerhardt, @maryamariyan Issue DetailsDescription.NET 6.0 introduced a =n undocumented breaking change. IHostedService startup is now special-cased for
When Example BackgroundService impl that breaksclass MyBackgroundService : BackgroundService
{
public override Task StartAsync(CancellationToken cancellation)
{
return Task.CompletedTask;
}
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
return Task.CompletedTask;
}
} Configuration
Regression?Yes, this is a breaking change in .NET 6. Previous versions did not special-case Other informationBreaking change was introduced in #42981 Expectations for this issueEither fix the regression, or document it as a breaking change in https://docs.microsoft.com/en-us/dotnet/core/compatibility/6.0
|
I think we can fix the NRE in this scenario for 6.0. |
In some scenarios, derived classes might not have called base.StartAsync, and ExecuteTask will still be null. Ensure we don't fail in those cases. Fix dotnet#60131
* Check BackgroundService.ExecuteTask for null In some scenarios, derived classes might not have called base.StartAsync, and ExecuteTask will still be null. Ensure we don't fail in those cases. Fix #60131
In some scenarios, derived classes might not have called base.StartAsync, and ExecuteTask will still be null. Ensure we don't fail in those cases. Fix #60131
Re-opening to backport to 6.0. |
* Check BackgroundService.ExecuteTask for null In some scenarios, derived classes might not have called base.StartAsync, and ExecuteTask will still be null. Ensure we don't fail in those cases. Fix #60131 * PR feedback Co-authored-by: Eric Erhardt <eric.erhardt@microsoft.com>
Fixed in 6.0 with #60177. |
Description
.NET 6.0 introduced an undocumented breaking change. IHostedService startup is now special-cased for
BackgroundService
, where the Host takes a direct dependency onBackgroundService.ExecuteTask
, which can be null in some cases that previously worked.runtime/src/libraries/Microsoft.Extensions.Hosting/src/Internal/Host.cs
Lines 80 to 84 in 6195d98
When
BackgroundService.ExecuteTask
is null (e.g. because the override forBackgroundService.StartAsync
decided not to call the base impl), this throws aNullReferenceException
and causes the Host to abort (instead of previous behavior whereIHostedService.StartAsync
was called and that was it.Example BackgroundService impl that breaks
Configuration
Regression?
Yes, this is a breaking change in .NET 6. Previous versions did not special-case
BackgroundService
, and instead treated allIHostedService
's uniformly. The new behavior IMHO violates the Principle of Least Astonishment.Other information
Breaking change was introduced in #42981
Expectations for this issue
Either fix the regression, or document it as a breaking change in https://docs.microsoft.com/en-us/dotnet/core/compatibility/6.0
The text was updated successfully, but these errors were encountered: