Add an ApplicationStarted event
This commit is contained in:
parent
ad93dd74b6
commit
4de328069a
|
|
@ -10,6 +10,12 @@ namespace Microsoft.AspNet.Hosting
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public interface IApplicationLifetime
|
public interface IApplicationLifetime
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Triggered when the application host has fully started and is about to wait
|
||||||
|
/// for a graceful shutdown.
|
||||||
|
/// </summary>
|
||||||
|
CancellationToken ApplicationStarted { get; }
|
||||||
|
|
||||||
/// <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.
|
||||||
|
|
|
||||||
|
|
@ -11,9 +11,19 @@ namespace Microsoft.AspNet.Hosting
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class ApplicationLifetime : IApplicationLifetime
|
public class ApplicationLifetime : IApplicationLifetime
|
||||||
{
|
{
|
||||||
|
private readonly CancellationTokenSource _startedSource = new CancellationTokenSource();
|
||||||
private readonly CancellationTokenSource _stoppingSource = new CancellationTokenSource();
|
private readonly CancellationTokenSource _stoppingSource = new CancellationTokenSource();
|
||||||
private readonly CancellationTokenSource _stoppedSource = new CancellationTokenSource();
|
private readonly CancellationTokenSource _stoppedSource = new CancellationTokenSource();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 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; }
|
||||||
|
}
|
||||||
|
|
||||||
/// <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.
|
||||||
|
|
@ -35,6 +45,21 @@ namespace Microsoft.AspNet.Hosting
|
||||||
get { return _stoppedSource.Token; }
|
get { return _stoppedSource.Token; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Signals the ApplicationStarted event and blocks until it completes.
|
||||||
|
/// </summary>
|
||||||
|
public void NotifyStarted()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_startedSource.Cancel(throwOnFirstException: false);
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
// TODO: LOG
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Signals the ApplicationStopping event and blocks until it completes.
|
/// Signals the ApplicationStopping event and blocks until it completes.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
||||||
|
|
@ -79,6 +79,8 @@ namespace Microsoft.AspNet.Hosting.Internal
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
_applicationLifetime.NotifyStarted();
|
||||||
|
|
||||||
return new Disposable(() =>
|
return new Disposable(() =>
|
||||||
{
|
{
|
||||||
_applicationLifetime.NotifyStopping();
|
_applicationLifetime.NotifyStopping();
|
||||||
|
|
|
||||||
|
|
@ -90,6 +90,21 @@ namespace Microsoft.AspNet.Hosting
|
||||||
Assert.Equal(1, _startInstances[0].DisposeCalls);
|
Assert.Equal(1, _startInstances[0].DisposeCalls);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void HostingEngineNotifiesApplicationStarted()
|
||||||
|
{
|
||||||
|
var host = CreateBuilder()
|
||||||
|
.UseServer(this)
|
||||||
|
.Build();
|
||||||
|
var applicationLifetime = host.ApplicationServices.GetRequiredService<IApplicationLifetime>();
|
||||||
|
|
||||||
|
Assert.False(applicationLifetime.ApplicationStarted.IsCancellationRequested);
|
||||||
|
using (host.Start())
|
||||||
|
{
|
||||||
|
Assert.True(applicationLifetime.ApplicationStarted.IsCancellationRequested);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void HostingEngineInjectsHostingEnvironment()
|
public void HostingEngineInjectsHostingEnvironment()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue