#434 Make sure IApplicationLifetime gets added even if startup throws.

This commit is contained in:
Chris R 2015-10-26 08:59:47 -07:00
parent 0107ba005b
commit 52796a09a2
2 changed files with 21 additions and 6 deletions

View File

@ -70,6 +70,7 @@ namespace Microsoft.AspNet.Hosting.Internal
_startupLoader = startupLoader;
_captureStartupErrors = captureStartupErrors;
_applicationLifetime = new ApplicationLifetime();
_applicationServiceCollection.AddInstance<IApplicationLifetime>(_applicationLifetime);
}
public IServiceProvider ApplicationServices
@ -144,7 +145,6 @@ namespace Microsoft.AspNet.Hosting.Internal
if (_applicationServices == null)
{
EnsureStartup();
_applicationServiceCollection.AddInstance<IApplicationLifetime>(_applicationLifetime);
_applicationServices = Startup.ConfigureServicesDelegate(_applicationServiceCollection);
}
}

View File

@ -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<StartupStaticCtorThrows>().Build();
var engine = builder.UseServer(serverFactory).UseStartup<StartupStaticCtorThrows>().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<StartupCtorThrows>().Build();
var engine = builder.UseServer(serverFactory).UseStartup<StartupCtorThrows>().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<StartupCtorThrows>().Build();
using (engine.Start())
{
var service = engine.ApplicationServices.GetService<IApplicationLifetime>();
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<StartupConfigureServicesThrows>().Build();
var engine = builder.UseServer(serverFactory).UseStartup<StartupConfigureServicesThrows>().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<StartupConfigureServicesThrows>().Build();
var engine = builder.UseServer(serverFactory).UseStartup<StartupConfigureServicesThrows>().Build();
using (engine.Start())
{
await AssertResponseContains(serverFactory.Application, "Exception from Configure");