From 25db4123324908bca1f417cba2b4ed9e23598306 Mon Sep 17 00:00:00 2001 From: Chris Ross Date: Fri, 30 May 2014 16:30:51 -0700 Subject: [PATCH] Clean up IApplicaitonLifetime service injection. --- src/Microsoft.AspNet.Hosting/HostingContext.cs | 1 - src/Microsoft.AspNet.Hosting/HostingEngine.cs | 17 +++-------------- src/Microsoft.AspNet.Hosting/HostingServices.cs | 2 ++ src/Microsoft.AspNet.Hosting/Program.cs | 2 +- 4 files changed, 6 insertions(+), 16 deletions(-) diff --git a/src/Microsoft.AspNet.Hosting/HostingContext.cs b/src/Microsoft.AspNet.Hosting/HostingContext.cs index 7ad428b722..ea5685e6bb 100644 --- a/src/Microsoft.AspNet.Hosting/HostingContext.cs +++ b/src/Microsoft.AspNet.Hosting/HostingContext.cs @@ -27,7 +27,6 @@ namespace Microsoft.AspNet.Hosting { public IServiceProvider Services { get; set; } public IConfiguration Configuration { get; set; } - public ApplicationLifetime Lifetime { get; set; } public IBuilder Builder { get; set; } diff --git a/src/Microsoft.AspNet.Hosting/HostingEngine.cs b/src/Microsoft.AspNet.Hosting/HostingEngine.cs index 7458f5713b..eac137bb38 100644 --- a/src/Microsoft.AspNet.Hosting/HostingEngine.cs +++ b/src/Microsoft.AspNet.Hosting/HostingEngine.cs @@ -46,35 +46,24 @@ namespace Microsoft.AspNet.Hosting public IDisposable Start(HostingContext context) { - EnsureLifetime(context); EnsureBuilder(context); EnsureServerFactory(context); InitalizeServerFactory(context); EnsureApplicationDelegate(context); + var applicationLifetime = (ApplicationLifetime)context.Services.GetService(); var pipeline = new PipelineInstance(_httpContextFactory, context.ApplicationDelegate); var server = context.ServerFactory.Start(context.Server, pipeline.Invoke); return new Disposable(() => { - context.Lifetime.SignalStopping(); + applicationLifetime.SignalStopping(); server.Dispose(); - context.Lifetime.SignalStopped(); pipeline.Dispose(); + applicationLifetime.SignalStopped(); }); } - private void EnsureLifetime(HostingContext context) - { - if (context.Lifetime == null) - { - context.Lifetime = new ApplicationLifetime(); - } - var serviceCollection = new ServiceCollection(); - serviceCollection.AddInstance(context.Lifetime); - context.Services = serviceCollection.BuildServiceProvider(context.Services); - } - private void EnsureBuilder(HostingContext context) { if (context.Builder != null) diff --git a/src/Microsoft.AspNet.Hosting/HostingServices.cs b/src/Microsoft.AspNet.Hosting/HostingServices.cs index c7376cd52e..e0995468d9 100644 --- a/src/Microsoft.AspNet.Hosting/HostingServices.cs +++ b/src/Microsoft.AspNet.Hosting/HostingServices.cs @@ -49,6 +49,8 @@ namespace Microsoft.AspNet.Hosting yield return describer.Transient(); + yield return describer.Instance(new ApplicationLifetime()); + // TODO: We expect this to be provide by the runtime eventually. yield return describer.Instance(new NullLoggerFactory()); diff --git a/src/Microsoft.AspNet.Hosting/Program.cs b/src/Microsoft.AspNet.Hosting/Program.cs index 8599267216..b1f694e018 100644 --- a/src/Microsoft.AspNet.Hosting/Program.cs +++ b/src/Microsoft.AspNet.Hosting/Program.cs @@ -84,7 +84,7 @@ namespace Microsoft.AspNet.Hosting shutdownHandle.Set(); }); - Task ignored = Task.Run(() => + var ignored = Task.Run(() => { Console.WriteLine("Started"); Console.ReadLine();