This repository has been archived by the owner on Dec 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 307
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Review feedback for IApplicationLifetimeEvents
- Renamed the type to IHostedService and added Start and Stop. - Split up the IHostedService execution and IApplicationLifetime to avoid circular references - Trigger IHostedService.Start after starting the server - Trigger IHostedService.Stop before disposing the service provider #895 #894
- Loading branch information
Showing
6 changed files
with
202 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/Microsoft.AspNetCore.Hosting/Internal/HostedServiceExecutor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Microsoft.AspNetCore.Hosting.Internal | ||
{ | ||
public class HostedServiceExecutor | ||
{ | ||
private readonly IEnumerable<IHostedService> _services; | ||
private readonly ILogger<HostedServiceExecutor> _logger; | ||
|
||
public HostedServiceExecutor(ILogger<HostedServiceExecutor> logger, IEnumerable<IHostedService> services) | ||
{ | ||
_logger = logger; | ||
_services = services; | ||
} | ||
|
||
public void Start() | ||
{ | ||
try | ||
{ | ||
Execute(service => service.Start()); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.ApplicationError(LoggerEventIds.HostedServiceStartException, "An error occurred starting the application", ex); | ||
} | ||
} | ||
|
||
public void Stop() | ||
{ | ||
try | ||
{ | ||
Execute(service => service.Stop()); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.ApplicationError(LoggerEventIds.HostedServiceStopException, "An error occurred stopping the application", ex); | ||
} | ||
} | ||
|
||
private void Execute(Action<IHostedService> callback) | ||
{ | ||
List<Exception> exceptions = null; | ||
|
||
foreach (var service in _services) | ||
{ | ||
try | ||
{ | ||
callback(service); | ||
} | ||
catch (Exception ex) | ||
{ | ||
if (exceptions == null) | ||
{ | ||
exceptions = new List<Exception>(); | ||
} | ||
|
||
exceptions.Add(ex); | ||
} | ||
} | ||
|
||
// Throw an aggregate exception if there were any exceptions | ||
if (exceptions != null) | ||
{ | ||
throw new AggregateException(exceptions); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.