Merge IApplicationShutdown and IApplicationLifetime
- Added StopApplication to IApplicationLifetime. This will replace IApplicationShutdown.RequestShutdown.
This commit is contained in:
parent
79a8a4e799
commit
374526b270
|
|
@ -30,5 +30,10 @@ namespace Microsoft.AspNet.Hosting
|
|||
/// </summary>
|
||||
/// <returns></returns>
|
||||
CancellationToken ApplicationStopped { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Requests termination the current application.
|
||||
/// </summary>
|
||||
void StopApplication();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue