Skip to content
This repository was archived by the owner on Dec 19, 2018. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public IWebHostBuilder ConfigureServices(Action<IServiceCollection> configureSer
throw new ArgumentNullException(nameof(configureServices));
}

return ConfigureServices((_ , services) => configureServices(services));
return ConfigureServices((_, services) => configureServices(services));
}

/// <summary>
Expand Down Expand Up @@ -189,10 +189,19 @@ public IWebHost Build()
_options,
_config,
hostingStartupErrors);
try
{
host.Initialize();

host.Initialize();

return host;
return host;
}
catch
{
// Dispose the host if there's a failure to initialize, this should clean up
// will dispose services that were constructed until the exception was thrown
host.Dispose();
throw;
}
}

private IServiceCollection BuildCommonServices(out AggregateException hostingStartupErrors)
Expand Down
45 changes: 45 additions & 0 deletions test/Microsoft.AspNetCore.Hosting.Tests/WebHostBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,23 @@ public void DoNotCaptureStartupErrorsByDefault()
Assert.Equal("A public method named 'ConfigureProduction' or 'Configure' could not be found in the 'Microsoft.AspNetCore.Hosting.Fakes.StartupBoom' type.", exception.Message);
}

[Fact]
public void ServiceProviderDisposedOnBuildException()
{
var service = new DisposableService();
var hostBuilder = new WebHostBuilder()
.UseServer(new TestServer())
.ConfigureServices(services =>
{
// Added as a factory since instances are never disposed by the container
services.AddSingleton(sp => service);
})
.UseStartup<StartupWithResolvedDisposableThatThrows>();

Assert.Throws<InvalidOperationException>(() => hostBuilder.Build());
Assert.True(service.Disposed);
}

[Fact]
public void CaptureStartupErrorsHonored()
{
Expand Down Expand Up @@ -1050,6 +1067,16 @@ public void Configure(IApplicationBuilder app)
}
}

public class DisposableService : IDisposable
{
public bool Disposed { get; private set; }

public void Dispose()
{
Disposed = true;
}
}

public class TestHostingStartup : IHostingStartup
{
public void Configure(IWebHostBuilder builder)
Expand All @@ -1068,6 +1095,24 @@ public void Configure(IWebHostBuilder builder)
}
}

public class StartupWithResolvedDisposableThatThrows
{
public StartupWithResolvedDisposableThatThrows(DisposableService service)
{

}

public void ConfigureServices(IServiceCollection services)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: public void ConfigureServices(IServiceCollection services) => throw new InvalidOperationException()

{
throw new InvalidOperationException();
}

public void Configure(IApplicationBuilder app)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: continue the trend where empty methods are shortened as public void Foo(...) {}?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that's a consistent trend.

{

}
}

public class TestLoggerProvider : ILoggerProvider
{
public TestSink Sink { get; set; } = new TestSink();
Expand Down