Skip to content

Commit 785d897

Browse files
committed
PR feedback
1 parent 15c827d commit 785d897

File tree

2 files changed

+20
-18
lines changed
  • src/libraries/Microsoft.Extensions.Hosting

2 files changed

+20
-18
lines changed

src/libraries/Microsoft.Extensions.Hosting/src/Internal/Host.cs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -81,27 +81,29 @@ private async Task TryExecuteBackgroundServiceAsync(BackgroundService background
8181
{
8282
// backgroundService.ExecuteTask may not be set (e.g. if the derived class doesn't call base.StartAsync)
8383
Task backgroundTask = backgroundService.ExecuteTask;
84-
if (backgroundTask != null)
84+
if (backgroundTask == null)
8585
{
86-
try
86+
return;
87+
}
88+
89+
try
90+
{
91+
await backgroundTask.ConfigureAwait(false);
92+
}
93+
catch (Exception ex)
94+
{
95+
// When the host is being stopped, it cancels the background services.
96+
// This isn't an error condition, so don't log it as an error.
97+
if (_stopCalled && backgroundTask.IsCanceled && ex is OperationCanceledException)
8798
{
88-
await backgroundTask.ConfigureAwait(false);
99+
return;
89100
}
90-
catch (Exception ex)
91-
{
92-
// When the host is being stopped, it cancels the background services.
93-
// This isn't an error condition, so don't log it as an error.
94-
if (_stopCalled && backgroundTask.IsCanceled && ex is OperationCanceledException)
95-
{
96-
return;
97-
}
98101

99-
_logger.BackgroundServiceFaulted(ex);
100-
if (_options.BackgroundServiceExceptionBehavior == BackgroundServiceExceptionBehavior.StopHost)
101-
{
102-
_logger.BackgroundServiceStoppingHost(ex);
103-
_applicationLifetime.StopApplication();
104-
}
102+
_logger.BackgroundServiceFaulted(ex);
103+
if (_options.BackgroundServiceExceptionBehavior == BackgroundServiceExceptionBehavior.StopHost)
104+
{
105+
_logger.BackgroundServiceStoppingHost(ex);
106+
_applicationLifetime.StopApplication();
105107
}
106108
}
107109
}

src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/Internal/HostTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1418,7 +1418,7 @@ public async Task StartOnBackgroundServiceThatDoesNotCallBase()
14181418

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

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

0 commit comments

Comments
 (0)