diff --git a/ServiceDeamon/App.config b/ServiceDeamon/App.config index 75622970..40b4dab8 100644 --- a/ServiceDeamon/App.config +++ b/ServiceDeamon/App.config @@ -1,7 +1,12 @@  + + + + + \ No newline at end of file diff --git a/ServiceDeamon/CheckServiceHelper.cs b/ServiceDeamon/CheckServiceHelper.cs index 0ba70891..04a2f429 100644 --- a/ServiceDeamon/CheckServiceHelper.cs +++ b/ServiceDeamon/CheckServiceHelper.cs @@ -10,25 +10,27 @@ namespace ServiceDeamon { public class CheckServiceHelper { - private static ServiceController servCtl; - private static string ServName = ConfigurationManager.AppSettings["ServiceName"]; + private static ServiceControllerStatus LastStatus; + public static void CheckStatus() { - if (servCtl == null || servCtl.ServiceName != ServName) + var servCtl = new ServiceController(ServName); + + if (LastStatus != servCtl.Status) { - servCtl = new ServiceController(ServName); + Console.WriteLine($"服务 {ServName} 的当前状态:{servCtl.Status}"); } - Console.WriteLine($"服务 {ServName} 的当前状态:{servCtl.Status}"); - if (servCtl != null && servCtl.Status == ServiceControllerStatus.Stopped) { Console.WriteLine($"服务已停止,自动启动:{ServName}"); servCtl.Start(); } + LastStatus = servCtl.Status; + //Console.WriteLine("CheckStatus"); } } diff --git a/ServiceDeamon/DeamonService.cs b/ServiceDeamon/DeamonService.cs new file mode 100644 index 00000000..9a31fe2c --- /dev/null +++ b/ServiceDeamon/DeamonService.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Topshelf; + +namespace ServiceDeamon +{ + public class DeamonService : ServiceControl + { + private static Timer timer; + private static int CheckInteval = Convert.ToInt32(ConfigurationManager.AppSettings["CheckInteval"]); + + + public bool Start(HostControl hostControl) + { + timer = new Timer(new TimerCallback(CheckSta)); + timer.Change(CheckInteval, Timeout.Infinite); + + return true; + } + + public bool Stop(HostControl hostControl) + { + timer.Change(-1, 0); + + return true; + } + + + + private static void CheckSta(object sta) + { + CheckServiceHelper.CheckStatus(); + + timer.Change(CheckInteval, Timeout.Infinite); + } + } +} diff --git a/ServiceDeamon/Program.cs b/ServiceDeamon/Program.cs index b4760fc8..13667c3b 100644 --- a/ServiceDeamon/Program.cs +++ b/ServiceDeamon/Program.cs @@ -1,27 +1,52 @@ using ServiceDeamon; using System.Configuration; using System.ServiceProcess; +using Topshelf; public class Program { - private static Timer timer; - - private static int CheckInteval = Convert.ToInt32(ConfigurationManager.AppSettings["CheckInteval"]); - public static void Main(string[] args) { - timer = new Timer(new TimerCallback(CheckSta)); - timer.Change(CheckInteval, Timeout.Infinite); - - - Console.Read(); - } - - private static void CheckSta(object sta) - { - CheckServiceHelper.CheckStatus(); - - timer.Change(CheckInteval, Timeout.Infinite); + 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); + } } }