using ServiceDeamon; using System.Configuration; using System.ServiceProcess; using Topshelf; public class Program { public static void Main(string[] args) { try { var serviceName = ConfigurationManager.AppSettings["DeamonServiceName"]; var serviceDisplayName = ConfigurationManager.AppSettings["DeamonServiceDisplayName"]; if (Environment.UserInteractive) { Console.Title = serviceDisplayName; } Host host = HostFactory.New(x => { // 基本的配置 x.RunAsLocalSystem(); x.SetServiceName(serviceName); x.SetDisplayName(serviceDisplayName); x.StartAutomaticallyDelayed(); x.EnableShutdown(); // 注册服务 x.Service(hostSettings => new DeamonService()); // 设置服务失败后的操作,分别对应第一次、第二次、后续 x.EnableServiceRecovery(t => { t.RestartService(0); t.RestartService(0); t.RestartService(0); t.OnCrashOnly(); }); }); host.Run(); } catch (Exception ex) { Console.WriteLine(ex); } } }