Skip to content
This repository has been archived by the owner on Dec 19, 2018. It is now read-only.

Commit

Permalink
#434 Make sure IApplicationLifetime gets added even if startup throws.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tratcher committed Oct 25, 2015
1 parent 0107ba0 commit 1f1c4b9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public HostingEngine(
_startupLoader = startupLoader;
_captureStartupErrors = captureStartupErrors;
_applicationLifetime = new ApplicationLifetime();
_applicationServiceCollection.AddInstance<IApplicationLifetime>(_applicationLifetime);
}

public IServiceProvider ApplicationServices
Expand Down Expand Up @@ -144,7 +145,6 @@ private void EnsureApplicationServices()
if (_applicationServices == null)
{
EnsureStartup();
_applicationServiceCollection.AddInstance<IApplicationLifetime>(_applicationLifetime);
_applicationServices = Startup.ConfigureServicesDelegate(_applicationServiceCollection);
}
}
Expand Down
15 changes: 15 additions & 0 deletions test/Microsoft.AspNet.Hosting.Tests/WebHostBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.AspNet.Http.Features;
using Microsoft.AspNet.Http.Internal;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace Microsoft.AspNet.Hosting
Expand Down Expand Up @@ -73,6 +74,20 @@ public async Task StartupCtorThrows_Fallback()
}
}

[Fact]
public async Task IApplicationLifetimeRegisteredEvenWhenStartupCtorThrows_Fallback()
{
var builder = CreateWebHostBuilder();
var serverFactory = new TestServerFactory();
var engine = (HostingEngine)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()
{
Expand Down

0 comments on commit 1f1c4b9

Please sign in to comment.