File tree Expand file tree Collapse file tree 2 files changed +20
-18
lines changed
src/libraries/Microsoft.Extensions.Hosting Expand file tree Collapse file tree 2 files changed +20
-18
lines changed Original file line number Diff line number Diff line change @@ -81,27 +81,29 @@ private async Task TryExecuteBackgroundServiceAsync(BackgroundService background
81
81
{
82
82
// backgroundService.ExecuteTask may not be set (e.g. if the derived class doesn't call base.StartAsync)
83
83
Task backgroundTask = backgroundService . ExecuteTask ;
84
- if ( backgroundTask ! = null )
84
+ if ( backgroundTask = = null )
85
85
{
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 )
87
98
{
88
- await backgroundTask . ConfigureAwait ( false ) ;
99
+ return ;
89
100
}
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
- }
98
101
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 ( ) ;
105
107
}
106
108
}
107
109
}
Original file line number Diff line number Diff line change @@ -1418,7 +1418,7 @@ public async Task StartOnBackgroundServiceThatDoesNotCallBase()
1418
1418
1419
1419
foreach ( LogEvent logEvent in logger . GetEvents ( ) )
1420
1420
{
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." ) ;
1422
1422
1423
1423
Assert . NotEqual ( "BackgroundServiceFaulted" , logEvent . EventId . Name ) ;
1424
1424
}
You can’t perform that action at this time.
0 commit comments