private void DoMonitor() { while (!_MonitorToken.IsCancellationRequested) { Thread.Sleep(10 * 1000); if (!_MonitorToken.IsCancellationRequested) { ServiceSlot[] slots = ServiceContext.Current.GetServiceSlots(); if (0 != slots.Length) { foreach (ServiceSlot s in slots) { if (_MonitorToken.IsCancellationRequested) { break; } if (s.WorkProcess.HasExited) { if (0 != s.WorkProcess.ExitCode) { "服务进程 {0} 已经退出 {1}".Formate(s.Name, s.WorkProcess.ExitCode.ToString()).Error(); ServiceContext.Current.RemoveSlot(s.WorkProcess.Id); if ("Y" == s.Config.RestartOnError.ToUpper()) { "服务进程 {0} 配置为在错误退出后重新启动".Formate(s.Name).Info(); string msg = ""; if (BasicServiceStarter.RunServiceProcess(ServiceContext.Current.Configuration.ServiceInfo.Name, s.Config, out msg)) { "服务进程 {0} 完成重启".Formate(s.Name).Info(); } else { msg.Error(); "服务进程 {0} 重启失败".Formate(s.Name).Error(); } } } else { "服务进程 {0} 已经退出".Formate(s.Name).Info(); } } } } } } }
public ActionResult StartService(string name) { ActionResult retValue = new ActionResult() { Result = -1 }; if (1 == _StartLockCount) { retValue.Message = "正在启动一个服务,请稍后再试"; return(retValue); } Interlocked.Increment(ref _StartLockCount); "尝试启动服务:{0}".Formate(name).Info(); ServiceSlot slot = ServiceContext.Current.ServiceSlots.FirstOrDefault(s => s.Name == name); ServiceStarterElement ele = ServiceContext.Current.Configuration.Services.Cast <ServiceStarterElement>().FirstOrDefault(s => s.Name == name); if (null != slot) { Interlocked.Decrement(ref _StartLockCount); retValue.Message = "服务:{0} 已经启动".Formate(name); retValue.Message.Info(); } else { string msg = ""; if (null != ele) { if (!BasicServiceStarter.RunServiceProcess(ServiceContext.Current.Configuration.ServiceInfo.Name, ele, out msg)) { retValue.Message = msg; msg.Error(); } else { retValue.Result = 0; } } else { retValue.Message = "找不到服务:{0} 的配置".Formate(name);; } Interlocked.Decrement(ref _StartLockCount); } return(retValue); }