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 IConfiguration Configuration { get; set; }
public ApplicationLifetime Lifetime { get; set; }
public IBuilder Builder { get; set; }

View File

@ -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<IApplicationLifetime>();
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<IApplicationLifetime>(context.Lifetime);
context.Services = serviceCollection.BuildServiceProvider(context.Services);
}
private void EnsureBuilder(HostingContext context)
{
if (context.Builder != null)

View File

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

View File

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