C# · 12月 21, 2021

[C#]通用守护进程服务

摘要<p style="margin-left: 30px;">很多情况下,都会使用windows服务做一些任务,但总会有一些异常,导致服务停止。这个时候,开发人员又不能立马解决问题,所以做一个守护者服务还是很有必要的。当检测到服务停止了,重启一下服务,等开发人员到位了,再排查错误日志。

代码<p style="margin-left: 30px;">app.config

value=/> value=/> value=/> <p style="margin-left: 30px;">服务

<span style=”color: #0000ff;”>namespace<span style=”color: #000000;”> WindowsService.Watch
{
<span style=”color: #0000ff;”>partial <span style=”color: #0000ff;”>class<span style=”color: #000000;”> ServiceWather : ServiceBase
{
<span style=”color: #0000ff;”>private <span style=”color: #0000ff;”>static <span style=”color: #0000ff;”>string currentExePath = <span style=”color: #0000ff;”>string<span style=”color: #000000;”>.Empty;
<span style=”color: #0000ff;”>public<span style=”color: #000000;”> ServiceWather()
{
InitializeComponent();
currentExePath =<span style=”color: #000000;”> AppDomain.CurrentDomain.BaseDirectory;
}
<span style=”color: #808080;”>/// <span style=”color: #808080;”>
<span style=”color: #808080;”>///<span style=”color: #008000;”> 检查间隔
<span style=”color: #808080;”>/// <span style=”color: #808080;”>
<span style=”color: #0000ff;”>private <span style=”color: #0000ff;”>static <span style=”color: #0000ff;”>readonly <span style=”color: #0000ff;”>int _timerInterval = Convert.ToInt32(ConfigurationManager.AppSettings[<span style=”color: #800000;”>”<span style=”color: #800000;”>timerInterval<span style=”color: #800000;”>”]) * <span style=”color: #800080;”>1000<span style=”color: #000000;”>;
<span style=”color: #808080;”>/// <span style=”color: #808080;”>
<span style=”color: #808080;”>///<span style=”color: #008000;”> 要守护的服务名
<span style=”color: #808080;”>/// <span style=”color: #808080;”>
<span style=”color: #0000ff;”>private <span style=”color: #0000ff;”>static <span style=”color: #0000ff;”>readonly <span style=”color: #0000ff;”>string toWatchServiceName = ConfigurationManager.AppSettings[<span style=”color: #800000;”>”<span style=”color: #800000;”>toWatchServiceName<span style=”color: #800000;”>”<span style=”color: #000000;”>];
<span style=”color: #0000ff;”>private<span style=”color: #000000;”> System.Timers.Timer _timer;
<span style=”color: #0000ff;”>protected <span style=”color: #0000ff;”>override <span style=”color: #0000ff;”>void OnStart(<span style=”color: #0000ff;”>string<span style=”color: #000000;”>[] args)
{
<span style=”color: #008000;”>//<span style=”color: #008000;”>服务启动时开启定时器
_timer = <span style=”color: #0000ff;”>new<span style=”color: #000000;”> System.Timers.Timer();
_timer.Interval =<span style=”color: #000000;”> _timerInterval;
_timer.Enabled = <span style=”color: #0000ff;”>true<span style=”color: #000000;”>;
_timer.AutoReset = <span style=”color: #0000ff;”>true<span style=”color: #000000;”>;
_timer.Elapsed +=<span style=”color: #000000;”> _timer_Elapsed;
LogHelper.WriteLog(currentExePath,<span style=”color: #800000;”>”<span style=”color: #800000;”>守护服务开启<span style=”color: #800000;”>”<span style=”color: #000000;”>);
}

</span><span style=”color: #0000ff;”&gt;void</span> _timer_Elapsed(<span style=”color: #0000ff;”&gt;object</span><span style=”color: #000000;”&gt; sender,Sy<a href=”https://www.jb51.cc/tag/stem/” target=”_blank” class=”keywords”>stem</a>.Timers.ElapsedEventArgs e) { </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;如果服务状态为停止,则重新启动服务</span> <span style=”color: #0000ff;”&gt;if</span> (!<span style=”color: #000000;”&gt;CheckSericeStart(toWatchServiceName)) { StartService(toWatchServiceName); } } </span><span style=”color: #0000ff;”&gt;protected</span> <span style=”color: #0000ff;”&gt;override</span> <span style=”color: #0000ff;”&gt;void</span><span style=”color: #000000;”&gt; OnStop() { </span><span style=”color: #0000ff;”&gt;if</span> (_timer != <span style=”color: #0000ff;”&gt;null</span><span style=”color: #000000;”&gt;) { _timer.Stop(); _timer.Dispose(); LogHelper.WriteLog(currentExePath,</span><span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;守护服务停止</span><span style=”color: #800000;”&gt;”</span><span style=”color: #000000;”&gt;); } } </span><span style=”color: #808080;”&gt;///</span> <span style=”color: #808080;”&gt;<summary></span> <span style=”color: #808080;”&gt;///</span><span style=”color: #008000;”&gt; 启动服务 </span><span style=”color: #808080;”&gt;///</span> <span style=”color: #808080;”&gt;</summary></span> <span style=”color: #808080;”&gt;///</span> <span style=”color: #808080;”&gt;<param name=”serviceName”&gt;</span><span style=”color: #008000;”&gt;要启动的服务<a href=”https://www.jb51.cc/tag/mingcheng/” target=”_blank” class=”keywords”>名称</a></span><span style=”color: #808080;”&gt;</param></span> <span style=”color: #0000ff;”&gt;private</span> <span style=”color: #0000ff;”&gt;void</span> StartService(<span style=”color: #0000ff;”&gt;string</span><span style=”color: #000000;”&gt; serviceName) { </span><span style=”color: #0000ff;”&gt;try</span><span style=”color: #000000;”&gt; { ServiceController[] services </span>=<span style=”color: #000000;”&gt; ServiceController.GetServices(); </span><span style=”color: #0000ff;”&gt;foreach</span> (ServiceController service <span style=”color: #0000ff;”&gt;in</span><span style=”color: #000000;”&gt; services) { </span><span style=”color: #0000ff;”&gt;if</span> (service.ServiceName.Trim() ==<span style=”color: #000000;”&gt; serviceName.Trim()) { service.Start(); </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;直到服务启动</span> service.WaitForStatus(ServiceControllerStatus.Running,<span style=”color: #0000ff;”&gt;new</span> TimeSpan(<span style=”color: #800080;”&gt;0</span>,<span style=”color: #800080;”&gt;0</span>,<span style=”color: #800080;”&gt;30</span><span style=”color: #000000;”&gt;)); LogHelper.WriteLog(currentExePath,</span><span style=”color: #0000ff;”&gt;string</span>.Format(<span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;启动服务:{0}</span><span style=”color: #800000;”&gt;”</span><span style=”color: #000000;”&gt;,serviceName)); } } } </span><span style=”color: #0000ff;”&gt;catch</span><span style=”color: #000000;”&gt; (Exception ex) { LogHelper.WriteLog(currentExePath,ex); } } </span><span style=”color: #0000ff;”&gt;private</span> <span style=”color: #0000ff;”&gt;bool</span> CheckSericeStart(<span style=”color: #0000ff;”&gt;string</span><span style=”color: #000000;”&gt; serviceName) { </span><span style=”color: #0000ff;”&gt;bool</span> result = <span style=”color: #0000ff;”&gt;true</span><span style=”color: #000000;”&gt;; </span><span style=”color: #0000ff;”&gt;try</span><span style=”color: #000000;”&gt; { ServiceController[] services </span>=<span style=”color: #000000;”&gt; ServiceController.GetServices(); </span><span style=”color: #0000ff;”&gt;foreach</span> (ServiceController service <span style=”color: #0000ff;”&gt;in</span><span style=”color: #000000;”&gt; services) { </span><span style=”color: #0000ff;”&gt;if</span> (service.ServiceName.Trim() ==<span style=”color: #000000;”&gt; serviceName.Trim()) { </span><span style=”color: #0000ff;”&gt;if</span> ((service.Status ==<span style=”color: #000000;”&gt; ServiceControllerStatus.Stopped) </span>|| (service.Status ==<span style=”color: #000000;”&gt; ServiceControllerStatus.StopPending)) { result </span>= <span style=”color: #0000ff;”&gt;false</span><span style=”color: #000000;”&gt;; } } } } </span><span style=”color: #0000ff;”&gt;catch</span><span style=”color: #000000;”&gt; (Exception ex) { LogHelper.WriteLog(currentExePath,ex); } </span><span style=”color: #0000ff;”&gt;return</span><span style=”color: #000000;”&gt; result; } </span><span style=”color: #808080;”&gt;///</span> <span style=”color: #808080;”&gt;<summary></span> <span style=”color: #808080;”&gt;///</span><span style=”color: #008000;”&gt; 停止 </span><span style=”color: #808080;”&gt;///</span> <span style=”color: #808080;”&gt;</summary></span> <span style=”color: #808080;”&gt;///</span> <span style=”color: #808080;”&gt;<param name=”serviceName”&gt;</param></span> <span style=”color: #0000ff;”&gt;private</span> <span style=”color: #0000ff;”&gt;void</span> StopService(<span style=”color: #0000ff;”&gt;string</span><span style=”color: #000000;”&gt; serviceName) { </span><span style=”color: #0000ff;”&gt;try</span><span style=”color: #000000;”&gt; { ServiceController[] services </span>=<span style=”color: #000000;”&gt; ServiceController.GetServices(); </span><span style=”color: #0000ff;”&gt;foreach</span> (ServiceController service <span style=”color: #0000ff;”&gt;in</span><span style=”color: #000000;”&gt; services) { </span><span style=”color: #0000ff;”&gt;if</span> (service.ServiceName.Trim() ==<span style=”color: #000000;”&gt; serviceName.Trim()) { service.Stop(); </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;直到服务停止</span> service.WaitForStatus(ServiceControllerStatus.Stopped,ex); } }}

}