diff --git a/test/Microsoft.AspNetCore.Server.KestrelTests/TestServer.cs b/test/Microsoft.AspNetCore.Server.KestrelTests/TestServer.cs index 6a84a29fd5..2e51e27b3e 100644 --- a/test/Microsoft.AspNetCore.Server.KestrelTests/TestServer.cs +++ b/test/Microsoft.AspNetCore.Server.KestrelTests/TestServer.cs @@ -33,20 +33,25 @@ namespace Microsoft.AspNetCore.Server.KestrelTests public int Port => _address.Port; public TestServer(RequestDelegate app, ServiceContext context, string serverAddress) - { - Create(app, context, serverAddress); - } - - public void Create(RequestDelegate app, ServiceContext context, string serverAddress) { context.FrameFactory = connectionContext => { return new Frame(new DummyApplication(app), connectionContext); }; - _engine = new KestrelEngine(context); - _engine.Start(1); - _address = ServerAddress.FromUrl(serverAddress); - _server = _engine.CreateServer(_address); + + try + { + _engine = new KestrelEngine(context); + _engine.Start(1); + _address = ServerAddress.FromUrl(serverAddress); + _server = _engine.CreateServer(_address); + } + catch + { + _server?.Dispose(); + _engine?.Dispose(); + throw; + } } public void Dispose()