diff --git a/src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs b/src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs index 6c0ad5eedd..6126d712ec 100644 --- a/src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs +++ b/src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs @@ -70,6 +70,7 @@ namespace Microsoft.AspNet.Hosting.Internal _startupLoader = startupLoader; _captureStartupErrors = captureStartupErrors; _applicationLifetime = new ApplicationLifetime(); + _applicationServiceCollection.AddInstance(_applicationLifetime); } public IServiceProvider ApplicationServices @@ -144,7 +145,6 @@ namespace Microsoft.AspNet.Hosting.Internal if (_applicationServices == null) { EnsureStartup(); - _applicationServiceCollection.AddInstance(_applicationLifetime); _applicationServices = Startup.ConfigureServicesDelegate(_applicationServiceCollection); } } diff --git a/test/Microsoft.AspNet.Hosting.Tests/WebHostBuilderTests.cs b/test/Microsoft.AspNet.Hosting.Tests/WebHostBuilderTests.cs index d558dd40e3..b8aa7c6e6c 100644 --- a/test/Microsoft.AspNet.Hosting.Tests/WebHostBuilderTests.cs +++ b/test/Microsoft.AspNet.Hosting.Tests/WebHostBuilderTests.cs @@ -11,6 +11,7 @@ using Microsoft.AspNet.Hosting.Server; using Microsoft.AspNet.Http.Features; using Microsoft.AspNet.Http.Internal; using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; using Xunit; namespace Microsoft.AspNet.Hosting @@ -42,7 +43,7 @@ namespace Microsoft.AspNet.Hosting { var builder = CreateWebHostBuilder(); var serverFactory = new TestServerFactory(); - var engine = (HostingEngine)builder.UseServer(serverFactory).UseStartup("MissingStartupAssembly").Build(); + var engine = builder.UseServer(serverFactory).UseStartup("MissingStartupAssembly").Build(); using (engine.Start()) { await AssertResponseContains(serverFactory.Application, "MissingStartupAssembly"); @@ -54,7 +55,7 @@ namespace Microsoft.AspNet.Hosting { var builder = CreateWebHostBuilder(); var serverFactory = new TestServerFactory(); - var engine = (HostingEngine)builder.UseServer(serverFactory).UseStartup().Build(); + var engine = builder.UseServer(serverFactory).UseStartup().Build(); using (engine.Start()) { await AssertResponseContains(serverFactory.Application, "Exception from static constructor"); @@ -66,19 +67,33 @@ namespace Microsoft.AspNet.Hosting { var builder = CreateWebHostBuilder(); var serverFactory = new TestServerFactory(); - var engine = (HostingEngine)builder.UseServer(serverFactory).UseStartup().Build(); + var engine = builder.UseServer(serverFactory).UseStartup().Build(); using (engine.Start()) { await AssertResponseContains(serverFactory.Application, "Exception from constructor"); } } + [Fact] + public async Task IApplicationLifetimeRegisteredEvenWhenStartupCtorThrows_Fallback() + { + var builder = CreateWebHostBuilder(); + var serverFactory = new TestServerFactory(); + var engine = builder.UseServer(serverFactory).UseStartup().Build(); + using (engine.Start()) + { + var service = engine.ApplicationServices.GetService(); + Assert.NotNull(service); + await AssertResponseContains(serverFactory.Application, "Exception from constructor"); + } + } + [Fact] public async Task StartupConfigureServicesThrows_Fallback() { var builder = CreateWebHostBuilder(); var serverFactory = new TestServerFactory(); - var engine = (HostingEngine)builder.UseServer(serverFactory).UseStartup().Build(); + var engine = builder.UseServer(serverFactory).UseStartup().Build(); using (engine.Start()) { await AssertResponseContains(serverFactory.Application, "Exception from ConfigureServices"); @@ -90,7 +105,7 @@ namespace Microsoft.AspNet.Hosting { var builder = CreateWebHostBuilder(); var serverFactory = new TestServerFactory(); - var engine = (HostingEngine)builder.UseServer(serverFactory).UseStartup().Build(); + var engine = builder.UseServer(serverFactory).UseStartup().Build(); using (engine.Start()) { await AssertResponseContains(serverFactory.Application, "Exception from Configure");