File tree Expand file tree Collapse file tree
src/libraries/Microsoft.Extensions.Hosting Expand file tree Collapse file tree Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff 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 }
You can’t perform that action at this time.
0 commit comments