diff --git a/src/Microsoft.AspNet.Hosting.Abstractions/IApplicationLifetime.cs b/src/Microsoft.AspNet.Hosting.Abstractions/IApplicationLifetime.cs index 536e24c165..3dd7bcf8aa 100644 --- a/src/Microsoft.AspNet.Hosting.Abstractions/IApplicationLifetime.cs +++ b/src/Microsoft.AspNet.Hosting.Abstractions/IApplicationLifetime.cs @@ -30,5 +30,10 @@ namespace Microsoft.AspNet.Hosting /// /// CancellationToken ApplicationStopped { get; } + + /// + /// Requests termination the current application. + /// + void StopApplication(); } } diff --git a/src/Microsoft.AspNet.Hosting/ApplicationLifetime.cs b/src/Microsoft.AspNet.Hosting/ApplicationLifetime.cs index ced615c889..230b4fd4e3 100644 --- a/src/Microsoft.AspNet.Hosting/ApplicationLifetime.cs +++ b/src/Microsoft.AspNet.Hosting/ApplicationLifetime.cs @@ -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. /// - public CancellationToken ApplicationStarted - { - get { return _startedSource.Token; } - } + public CancellationToken ApplicationStarted => _startedSource.Token; /// /// Triggered when the application host is performing a graceful shutdown. /// Request may still be in flight. Shutdown will block until this event completes. /// /// - public CancellationToken ApplicationStopping - { - get { return _stoppingSource.Token; } - } + public CancellationToken ApplicationStopping => _stoppingSource.Token; /// /// Triggered when the application host is performing a graceful shutdown. @@ -40,9 +34,21 @@ namespace Microsoft.AspNet.Hosting /// until this event completes. /// /// - public CancellationToken ApplicationStopped + public CancellationToken ApplicationStopped => _stoppedSource.Token; + + /// + /// Signals the ApplicationStopping event and blocks until it completes. + /// + public void StopApplication() { - get { return _stoppedSource.Token; } + try + { + _stoppingSource.Cancel(throwOnFirstException: false); + } + catch (Exception) + { + // TODO: LOG + } } /// @@ -60,21 +66,6 @@ namespace Microsoft.AspNet.Hosting } } - /// - /// Signals the ApplicationStopping event and blocks until it completes. - /// - public void NotifyStopping() - { - try - { - _stoppingSource.Cancel(throwOnFirstException: false); - } - catch (Exception) - { - // TODO: LOG - } - } - /// /// Signals the ApplicationStopped event and blocks until it completes. /// diff --git a/src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs b/src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs index d0a951849d..cde9a27c1c 100644 --- a/src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs +++ b/src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs @@ -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(); diff --git a/src/Microsoft.AspNet.Hosting/Program.cs b/src/Microsoft.AspNet.Hosting/Program.cs index 7c2cbb2127..48a71bbcde 100644 --- a/src/Microsoft.AspNet.Hosting/Program.cs +++ b/src/Microsoft.AspNet.Hosting/Program.cs @@ -54,14 +54,16 @@ namespace Microsoft.AspNet.Hosting Console.WriteLine("Application started. Press Ctrl+C to shut down."); - var appShutdownService = app.Services.GetRequiredService(); + var appLifetime = app.Services.GetRequiredService(); + 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(); } } }