Skip to content
This repository has been archived by the owner on Dec 19, 2018. It is now read-only.

Commit

Permalink
Simplify Hosting's shutdown handling.
Browse files Browse the repository at this point in the history
Don't require a TTY on Unix.
  • Loading branch information
Kai Ruhnau authored and Tragetaschen committed May 11, 2015
1 parent 12d3f48 commit fcf4573
Showing 1 changed file with 5 additions and 29 deletions.
34 changes: 5 additions & 29 deletions src/Microsoft.AspNet.Hosting/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,33 +32,12 @@ public void Main(string[] args)
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(() =>
{
try
{
serverShutdown.Dispose();
}
catch (Exception ex)
{
var logger = loggerFactory.CreateLogger<Program>();
logger.LogError("Dispose threw an exception.", ex);
}
shutdownHandle.Set();
});

var ignored = Task.Run(() =>
using (host.Start())
{
Console.WriteLine("Started");
Console.ReadLine();
appShutdownService.RequestShutdown();
});

shutdownHandle.WaitOne();
var appShutdownService = host.ApplicationServices.GetRequiredService<IApplicationShutdown>();
Console.CancelKeyPress += delegate { appShutdownService.RequestShutdown(); };
appShutdownService.ShutdownRequested.WaitHandle.WaitOne();
}
}
}
}

0 comments on commit fcf4573

Please sign in to comment.