当我在控制台中调试它时,我希望 Topshelf 停止我的服务。
我的代码:
public class Program
{
private static ILog _log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public static void Main(string[] args)
{
HostFactory.Run(Configure);
}
/// <summary>
/// Configures the service host.
/// </summary>
/// <param name="cfg">The HostConfigurator</param>
private static void Configure(HostConfigurator cfg)
{
try
{
cfg.Service<ServiceHost>(s =>
{
s.ConstructUsing(name => new ServiceHost());
s.WhenStarted(host => host.StartService());
s.WhenStopped(host => host.StopService());
});
cfg.RunAsLocalSystem();
cfg.StartManually();
cfg.SetDisplayName(DisplayName);
cfg.SetServiceName(ServiceName);
cfg.UseLog4Net();
}
catch (Exception ex)
{
_log.Fatal("Exception on Startup", ex);
throw;
}
}
}
但是当我按下CTRL+C它时它会挂起Microsoft.VisualStudio.HostingProcess.HostProc.WaitForThreadExit
。
当我按下时CTRL+C,我希望host => host.StopService()
立即调用它。
那可能吗?