Merge IApplicationShutdown and IApplicationLifetime

- Added StopApplication to IApplicationLifetime. This will replace
IApplicationShutdown.RequestShutdown.
This commit is contained in:
David Fowler 2015-10-13 03:31:33 -07:00
parent 79a8a4e799
commit 374526b270
4 changed files with 27 additions and 29 deletions

View File

@ -30,5 +30,10 @@ namespace Microsoft.AspNet.Hosting
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
CancellationToken ApplicationStopped { get; } CancellationToken ApplicationStopped { get; }
/// <summary>
/// Requests termination the current application.
/// </summary>
void StopApplication();
} }
} }

View File

@ -19,20 +19,14 @@ namespace Microsoft.AspNet.Hosting
/// Triggered when the application host has fully started and is about to wait /// Triggered when the application host has fully started and is about to wait
/// for a graceful shutdown. /// for a graceful shutdown.
/// </summary> /// </summary>
public CancellationToken ApplicationStarted public CancellationToken ApplicationStarted => _startedSource.Token;
{
get { return _startedSource.Token; }
}
/// <summary> /// <summary>
/// Triggered when the application host is performing a graceful shutdown. /// Triggered when the application host is performing a graceful shutdown.
/// Request may still be in flight. Shutdown will block until this event completes. /// Request may still be in flight. Shutdown will block until this event completes.
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public CancellationToken ApplicationStopping public CancellationToken ApplicationStopping => _stoppingSource.Token;
{
get { return _stoppingSource.Token; }
}
/// <summary> /// <summary>
/// Triggered when the application host is performing a graceful shutdown. /// Triggered when the application host is performing a graceful shutdown.
@ -40,9 +34,21 @@ namespace Microsoft.AspNet.Hosting
/// until this event completes. /// until this event completes.
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public CancellationToken ApplicationStopped public CancellationToken ApplicationStopped => _stoppedSource.Token;
/// <summary>
/// Signals the ApplicationStopping event and blocks until it completes.
/// </summary>
public void StopApplication()
{ {
get { return _stoppedSource.Token; } try
{
_stoppingSource.Cancel(throwOnFirstException: false);
}
catch (Exception)
{
// TODO: LOG
}
} }
/// <summary> /// <summary>
@ -60,21 +66,6 @@ namespace Microsoft.AspNet.Hosting
} }
} }
/// <summary>
/// Signals the ApplicationStopping event and blocks until it completes.
/// </summary>
public void NotifyStopping()
{
try
{
_stoppingSource.Cancel(throwOnFirstException: false);
}
catch (Exception)
{
// TODO: LOG
}
}
/// <summary> /// <summary>
/// Signals the ApplicationStopped event and blocks until it completes. /// Signals the ApplicationStopped event and blocks until it completes.
/// </summary> /// </summary>

View File

@ -135,7 +135,7 @@ namespace Microsoft.AspNet.Hosting.Internal
return new Application(ApplicationServices, _serverInstance, new Disposable(() => return new Application(ApplicationServices, _serverInstance, new Disposable(() =>
{ {
_applicationLifetime.NotifyStopping(); _applicationLifetime.StopApplication();
server.Dispose(); server.Dispose();
_applicationLifetime.NotifyStopped(); _applicationLifetime.NotifyStopped();
(_applicationServices as IDisposable)?.Dispose(); (_applicationServices as IDisposable)?.Dispose();

View File

@ -54,14 +54,16 @@ namespace Microsoft.AspNet.Hosting
Console.WriteLine("Application started. Press Ctrl+C to shut down."); Console.WriteLine("Application started. Press Ctrl+C to shut down.");
var appShutdownService = app.Services.GetRequiredService<IApplicationShutdown>(); var appLifetime = app.Services.GetRequiredService<IApplicationLifetime>();
Console.CancelKeyPress += (sender, eventArgs) => Console.CancelKeyPress += (sender, eventArgs) =>
{ {
appShutdownService.RequestShutdown(); appLifetime.StopApplication();
// Don't terminate the process immediately, wait for the Main thread to exit gracefully. // Don't terminate the process immediately, wait for the Main thread to exit gracefully.
eventArgs.Cancel = true; eventArgs.Cancel = true;
}; };
appShutdownService.ShutdownRequested.WaitHandle.WaitOne();
appLifetime.ApplicationStopping.WaitHandle.WaitOne();
} }
} }
} }