// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Threading; namespace Microsoft.AspNet.Hosting { /// /// Allows consumers to perform cleanup during a graceful shutdown. /// public class ApplicationLifetime : IApplicationLifetime { private readonly CancellationTokenSource _startedSource = new CancellationTokenSource(); private readonly CancellationTokenSource _stoppingSource = new CancellationTokenSource(); private readonly CancellationTokenSource _stoppedSource = new CancellationTokenSource(); /// /// Triggered when the application host has fully started and is about to wait /// for a graceful shutdown. /// public CancellationToken ApplicationStarted { get { return _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; } } /// /// Triggered when the application host is performing a graceful shutdown. /// All requests should be complete at this point. Shutdown will block /// until this event completes. /// /// public CancellationToken ApplicationStopped { get { return _stoppedSource.Token; } } /// /// Signals the ApplicationStarted event and blocks until it completes. /// public void NotifyStarted() { try { _startedSource.Cancel(throwOnFirstException: false); } catch (Exception) { // TODO: LOG } } /// /// 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. /// public void NotifyStopped() { try { _stoppedSource.Cancel(throwOnFirstException: false); } catch (Exception) { // TODO: LOG } } } }