Add an ApplicationStarted event
This commit is contained in:
parent
ad93dd74b6
commit
4de328069a
|
|
@ -10,6 +10,12 @@ namespace Microsoft.AspNet.Hosting
|
|||
/// </summary>
|
||||
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>
|
||||
/// Triggered when the application host is performing a graceful shutdown.
|
||||
/// Request may still be in flight. Shutdown will block until this event completes.
|
||||
|
|
@ -25,4 +31,4 @@ namespace Microsoft.AspNet.Hosting
|
|||
/// <returns></returns>
|
||||
CancellationToken ApplicationStopped { get; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,9 +11,19 @@ namespace Microsoft.AspNet.Hosting
|
|||
/// </summary>
|
||||
public class ApplicationLifetime : IApplicationLifetime
|
||||
{
|
||||
private readonly CancellationTokenSource _startedSource = new CancellationTokenSource();
|
||||
private readonly CancellationTokenSource _stoppingSource = 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>
|
||||
/// Triggered when the application host is performing a graceful shutdown.
|
||||
/// 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; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Signals the ApplicationStarted event and blocks until it completes.
|
||||
/// </summary>
|
||||
public void NotifyStarted()
|
||||
{
|
||||
try
|
||||
{
|
||||
_startedSource.Cancel(throwOnFirstException: false);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// TODO: LOG
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Signals the ApplicationStopping event and blocks until it completes.
|
||||
/// </summary>
|
||||
|
|
@ -65,4 +90,4 @@ namespace Microsoft.AspNet.Hosting
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -79,6 +79,8 @@ namespace Microsoft.AspNet.Hosting.Internal
|
|||
}
|
||||
});
|
||||
|
||||
_applicationLifetime.NotifyStarted();
|
||||
|
||||
return new Disposable(() =>
|
||||
{
|
||||
_applicationLifetime.NotifyStopping();
|
||||
|
|
@ -182,4 +184,4 @@ namespace Microsoft.AspNet.Hosting.Internal
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -90,6 +90,21 @@ namespace Microsoft.AspNet.Hosting
|
|||
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]
|
||||
public void HostingEngineInjectsHostingEnvironment()
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue