Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/libraries/Microsoft.Extensions.Hosting/src/Internal/Host.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,22 @@ public async Task StartAsync(CancellationToken cancellationToken = default)

private async Task TryExecuteBackgroundServiceAsync(BackgroundService backgroundService)
{
// backgroundService.ExecuteTask may not be set (e.g. if the derived class doesn't call base.StartAsync)
Task backgroundTask = backgroundService.ExecuteTask;
if (backgroundTask == null)
{
return;
}

try
{
await backgroundService.ExecuteTask.ConfigureAwait(false);
await backgroundTask.ConfigureAwait(false);
}
catch (Exception ex)
{
// When the host is being stopped, it cancels the background services.
// This isn't an error condition, so don't log it as an error.
if (_stopCalled && backgroundService.ExecuteTask.IsCanceled && ex is OperationCanceledException)
if (_stopCalled && backgroundTask.IsCanceled && ex is OperationCanceledException)
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1394,6 +1394,36 @@ public async Task HostNoErrorWhenServiceIsCanceledAsPartOfStop()
}
}

/// <summary>
/// Tests that when a BackgroundService does not call base, the Host still starts and stops successfully.
/// </summary>
[Fact]
public async Task StartOnBackgroundServiceThatDoesNotCallBase()
{
TestLoggerProvider logger = new TestLoggerProvider();

using IHost host = CreateBuilder()
.ConfigureLogging(logging =>
{
logging.AddProvider(logger);
})
.ConfigureServices(services =>
{
services.AddHostedService<BackgroundServiceDoesNotCallBase>();
})
.Build();

host.Start();
await host.StopAsync();

foreach (LogEvent logEvent in logger.GetEvents())
{
Assert.True(logEvent.LogLevel <= LogLevel.Information, "All logged events should be less than or equal to Information. No Warnings or Errors.");

Assert.NotEqual("BackgroundServiceFaulted", logEvent.EventId.Name);
}
}

private IHostBuilder CreateBuilder(IConfiguration config = null)
{
return new HostBuilder().ConfigureHostConfiguration(builder => builder.AddConfiguration(config ?? new ConfigurationBuilder().Build()));
Expand Down Expand Up @@ -1562,5 +1592,14 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
}
}
}

private class BackgroundServiceDoesNotCallBase : BackgroundService
{
public override Task StartAsync(CancellationToken cancellationToken) => Task.CompletedTask;

protected override Task ExecuteAsync(CancellationToken stoppingToken) => Task.CompletedTask;

public override Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}
}
}