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>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
CancellationToken ApplicationStopped { get; }
|
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
|
/// 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>
|
||||||
|
|
|
||||||
|
|
@ -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();
|
||||||
|
|
|
||||||
|
|
@ -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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue