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>
/// <returns></returns>
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
/// for a graceful shutdown.
/// </summary>
public CancellationToken ApplicationStarted
{
get { return _startedSource.Token; }
}
public CancellationToken ApplicationStarted => _startedSource.Token;
/// <summary>
/// Triggered when the application host is performing a graceful shutdown.
/// Request may still be in flight. Shutdown will block until this event completes.
/// </summary>
/// <returns></returns>
public CancellationToken ApplicationStopping
{
get { return _stoppingSource.Token; }
}
public CancellationToken ApplicationStopping => _stoppingSource.Token;
/// <summary>
/// Triggered when the application host is performing a graceful shutdown.
@ -40,9 +34,21 @@ namespace Microsoft.AspNet.Hosting
/// until this event completes.
/// </summary>
/// <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>
@ -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>
/// Signals the ApplicationStopped event and blocks until it completes.
/// </summary>

View File

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

View File

@ -54,14 +54,16 @@ namespace Microsoft.AspNet.Hosting
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) =>
{
appShutdownService.RequestShutdown();
appLifetime.StopApplication();
// Don't terminate the process immediately, wait for the Main thread to exit gracefully.
eventArgs.Cancel = true;
};
appShutdownService.ShutdownRequested.WaitHandle.WaitOne();
appLifetime.ApplicationStopping.WaitHandle.WaitOne();
}
}
}