Clean up if an exception is thrown in the middle of ServerFactory.Start
This commit is contained in:
parent
c50aec1729
commit
1584d70e1f
|
|
@ -36,30 +36,44 @@ namespace Microsoft.AspNet.Server.Kestrel
|
||||||
|
|
||||||
public IDisposable Start(IFeatureCollection serverFeatures, Func<IFeatureCollection, Task> application)
|
public IDisposable Start(IFeatureCollection serverFeatures, Func<IFeatureCollection, Task> application)
|
||||||
{
|
{
|
||||||
var disposables = new List<IDisposable>();
|
var disposables = new Stack<IDisposable>();
|
||||||
var information = (KestrelServerInformation)serverFeatures.Get<IKestrelServerInformation>();
|
var disposer = new Disposable(() =>
|
||||||
var engine = new KestrelEngine(_libraryManager, _appShutdownService);
|
|
||||||
engine.Start(information.ThreadCount == 0 ? 1 : information.ThreadCount);
|
|
||||||
foreach (var address in information.Addresses)
|
|
||||||
{
|
|
||||||
disposables.Add(engine.CreateServer(
|
|
||||||
address.Scheme,
|
|
||||||
address.Host,
|
|
||||||
address.Port,
|
|
||||||
async frame =>
|
|
||||||
{
|
|
||||||
var request = new ServerRequest(frame);
|
|
||||||
await application.Invoke(request.Features).ConfigureAwait(false);
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
disposables.Add(engine);
|
|
||||||
return new Disposable(() =>
|
|
||||||
{
|
{
|
||||||
foreach (var disposable in disposables)
|
foreach (var disposable in disposables)
|
||||||
{
|
{
|
||||||
disposable.Dispose();
|
disposable.Dispose();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var information = (KestrelServerInformation)serverFeatures.Get<IKestrelServerInformation>();
|
||||||
|
var engine = new KestrelEngine(_libraryManager, _appShutdownService);
|
||||||
|
|
||||||
|
disposables.Push(engine);
|
||||||
|
|
||||||
|
engine.Start(information.ThreadCount == 0 ? 1 : information.ThreadCount);
|
||||||
|
|
||||||
|
foreach (var address in information.Addresses)
|
||||||
|
{
|
||||||
|
disposables.Push(engine.CreateServer(
|
||||||
|
address.Scheme,
|
||||||
|
address.Host,
|
||||||
|
address.Port,
|
||||||
|
async frame =>
|
||||||
|
{
|
||||||
|
var request = new ServerRequest(frame);
|
||||||
|
await application.Invoke(request.Features).ConfigureAwait(false);
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
return disposer;
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
disposer.Dispose();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue