Clean up IApplicaitonLifetime service injection.

This commit is contained in:
Chris Ross 2014-05-30 16:30:51 -07:00
parent 43ae61f7bc
commit 25db412332
4 changed files with 6 additions and 16 deletions

View File

@ -27,7 +27,6 @@ namespace Microsoft.AspNet.Hosting
{ {
public IServiceProvider Services { get; set; } public IServiceProvider Services { get; set; }
public IConfiguration Configuration { get; set; } public IConfiguration Configuration { get; set; }
public ApplicationLifetime Lifetime { get; set; }
public IBuilder Builder { get; set; } public IBuilder Builder { get; set; }

View File

@ -46,35 +46,24 @@ namespace Microsoft.AspNet.Hosting
public IDisposable Start(HostingContext context) public IDisposable Start(HostingContext context)
{ {
EnsureLifetime(context);
EnsureBuilder(context); EnsureBuilder(context);
EnsureServerFactory(context); EnsureServerFactory(context);
InitalizeServerFactory(context); InitalizeServerFactory(context);
EnsureApplicationDelegate(context); EnsureApplicationDelegate(context);
var applicationLifetime = (ApplicationLifetime)context.Services.GetService<IApplicationLifetime>();
var pipeline = new PipelineInstance(_httpContextFactory, context.ApplicationDelegate); var pipeline = new PipelineInstance(_httpContextFactory, context.ApplicationDelegate);
var server = context.ServerFactory.Start(context.Server, pipeline.Invoke); var server = context.ServerFactory.Start(context.Server, pipeline.Invoke);
return new Disposable(() => return new Disposable(() =>
{ {
context.Lifetime.SignalStopping(); applicationLifetime.SignalStopping();
server.Dispose(); server.Dispose();
context.Lifetime.SignalStopped();
pipeline.Dispose(); pipeline.Dispose();
applicationLifetime.SignalStopped();
}); });
} }
private void EnsureLifetime(HostingContext context)
{
if (context.Lifetime == null)
{
context.Lifetime = new ApplicationLifetime();
}
var serviceCollection = new ServiceCollection();
serviceCollection.AddInstance<IApplicationLifetime>(context.Lifetime);
context.Services = serviceCollection.BuildServiceProvider(context.Services);
}
private void EnsureBuilder(HostingContext context) private void EnsureBuilder(HostingContext context)
{ {
if (context.Builder != null) if (context.Builder != null)

View File

@ -49,6 +49,8 @@ namespace Microsoft.AspNet.Hosting
yield return describer.Transient<ITypeActivator, TypeActivator>(); yield return describer.Transient<ITypeActivator, TypeActivator>();
yield return describer.Instance<IApplicationLifetime>(new ApplicationLifetime());
// TODO: We expect this to be provide by the runtime eventually. // TODO: We expect this to be provide by the runtime eventually.
yield return describer.Instance<ILoggerFactory>(new NullLoggerFactory()); yield return describer.Instance<ILoggerFactory>(new NullLoggerFactory());

View File

@ -84,7 +84,7 @@ namespace Microsoft.AspNet.Hosting
shutdownHandle.Set(); shutdownHandle.Set();
}); });
Task ignored = Task.Run(() => var ignored = Task.Run(() =>
{ {
Console.WriteLine("Started"); Console.WriteLine("Started");
Console.ReadLine(); Console.ReadLine();