Simplify Hosting's shutdown handling.

Don't require a TTY on Unix.
This commit is contained in:
Kai Ruhnau 2015-05-07 11:25:57 +02:00 committed by Kai Ruhnau
parent 1354d66fb6
commit 95b1997c14
1 changed files with 5 additions and 29 deletions

View File

@ -3,12 +3,9 @@
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNet.Hosting.Internal;
using Microsoft.Framework.ConfigurationModel;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging;
using Microsoft.Framework.Runtime;
namespace Microsoft.AspNet.Hosting
@ -35,33 +32,12 @@ namespace Microsoft.AspNet.Hosting
config.AddCommandLine(args);
var host = new WebHostBuilder(_serviceProvider, config).Build();
var serverShutdown = host.Start();
var loggerFactory = host.ApplicationServices.GetRequiredService<ILoggerFactory>();
var appShutdownService = host.ApplicationServices.GetRequiredService<IApplicationShutdown>();
var shutdownHandle = new ManualResetEvent(false);
appShutdownService.ShutdownRequested.Register(() =>
using (host.Start())
{
try
{
serverShutdown.Dispose();
}
catch (Exception ex)
{
var logger = loggerFactory.CreateLogger<Program>();
logger.LogError("Dispose threw an exception.", ex);
}
shutdownHandle.Set();
});
var ignored = Task.Run(() =>
{
Console.WriteLine("Started");
Console.ReadLine();
appShutdownService.RequestShutdown();
});
shutdownHandle.WaitOne();
var appShutdownService = host.ApplicationServices.GetRequiredService<IApplicationShutdown>();
Console.CancelKeyPress += delegate { appShutdownService.RequestShutdown(); };
appShutdownService.ShutdownRequested.WaitHandle.WaitOne();
}
}
}
}