Dispose the host if Initialize fails (#1324)
This commit is contained in:
parent
86989413f6
commit
f8d61a4c52
|
|
@ -100,7 +100,7 @@ namespace Microsoft.AspNetCore.Hosting
|
||||||
throw new ArgumentNullException(nameof(configureServices));
|
throw new ArgumentNullException(nameof(configureServices));
|
||||||
}
|
}
|
||||||
|
|
||||||
return ConfigureServices((_ , services) => configureServices(services));
|
return ConfigureServices((_, services) => configureServices(services));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -189,10 +189,19 @@ namespace Microsoft.AspNetCore.Hosting
|
||||||
_options,
|
_options,
|
||||||
_config,
|
_config,
|
||||||
hostingStartupErrors);
|
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)
|
private IServiceCollection BuildCommonServices(out AggregateException hostingStartupErrors)
|
||||||
|
|
|
||||||
|
|
@ -369,6 +369,23 @@ namespace Microsoft.AspNetCore.Hosting
|
||||||
Assert.Equal("A public method named 'ConfigureProduction' or 'Configure' could not be found in the 'Microsoft.AspNetCore.Hosting.Fakes.StartupBoom' type.", exception.Message);
|
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]
|
[Fact]
|
||||||
public void CaptureStartupErrorsHonored()
|
public void CaptureStartupErrorsHonored()
|
||||||
{
|
{
|
||||||
|
|
@ -1050,6 +1067,16 @@ namespace Microsoft.AspNetCore.Hosting
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class DisposableService : IDisposable
|
||||||
|
{
|
||||||
|
public bool Disposed { get; private set; }
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
Disposed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public class TestHostingStartup : IHostingStartup
|
public class TestHostingStartup : IHostingStartup
|
||||||
{
|
{
|
||||||
public void Configure(IWebHostBuilder builder)
|
public void Configure(IWebHostBuilder builder)
|
||||||
|
|
@ -1068,6 +1095,24 @@ namespace Microsoft.AspNetCore.Hosting
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class StartupWithResolvedDisposableThatThrows
|
||||||
|
{
|
||||||
|
public StartupWithResolvedDisposableThatThrows(DisposableService service)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ConfigureServices(IServiceCollection services)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Configure(IApplicationBuilder app)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public class TestLoggerProvider : ILoggerProvider
|
public class TestLoggerProvider : ILoggerProvider
|
||||||
{
|
{
|
||||||
public TestSink Sink { get; set; } = new TestSink();
|
public TestSink Sink { get; set; } = new TestSink();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue