Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
PR feedback
  • Loading branch information
eerhardt authored and github-actions committed Oct 8, 2021
commit 07fe4ac8b64ee2cb43eaf1a6d16c94cc20584a48
36 changes: 19 additions & 17 deletions src/libraries/Microsoft.Extensions.Hosting/src/Internal/Host.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,27 +81,29 @@ private async Task TryExecuteBackgroundServiceAsync(BackgroundService background
{
// backgroundService.ExecuteTask may not be set (e.g. if the derived class doesn't call base.StartAsync)
Task backgroundTask = backgroundService.ExecuteTask;
if (backgroundTask != null)
if (backgroundTask == null)
{
try
return;
}

try
{
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 && backgroundTask.IsCanceled && ex is OperationCanceledException)
{
await backgroundTask.ConfigureAwait(false);
return;
}
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 && backgroundTask.IsCanceled && ex is OperationCanceledException)
{
return;
}

_logger.BackgroundServiceFaulted(ex);
if (_options.BackgroundServiceExceptionBehavior == BackgroundServiceExceptionBehavior.StopHost)
{
_logger.BackgroundServiceStoppingHost(ex);
_applicationLifetime.StopApplication();
}
_logger.BackgroundServiceFaulted(ex);
if (_options.BackgroundServiceExceptionBehavior == BackgroundServiceExceptionBehavior.StopHost)
{
_logger.BackgroundServiceStoppingHost(ex);
_applicationLifetime.StopApplication();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1418,7 +1418,7 @@ public async Task StartOnBackgroundServiceThatDoesNotCallBase()

foreach (LogEvent logEvent in logger.GetEvents())
{
Assert.True(logEvent.LogLevel <= LogLevel.Information, "All logged events should be les than or equal to Information. No Warnings or Errors.");
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);
}
Expand Down