protected override void OnStart(string[] args) { "启动监听服务".Info(); CStarterDControlServiceDaemon.Current.Start(_Config); CStarterDNotifierServiceDaemon.Current.Start(_Config); "监听服务已经启动".Info(); BasicServiceStarter.Run(_Config); _StarterMonitor = new CStarterMonitor(); _StarterMonitor.StartMonitor(); }
static void Main(string[] args) { ServiceStarterParams param = new ServiceStarterParams(); List<string> extra = param.Parse(args); extra = param.Parse(args); Configuration config; if (!string.IsNullOrEmpty(param.ConfigurationFileName)) { string fileFullName = param.ConfigurationFileName; if (!Path.IsPathRooted(fileFullName)) { fileFullName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, param.ConfigurationFileName); } ExeConfigurationFileMap map = new ExeConfigurationFileMap(); map.ExeConfigFilename = fileFullName; config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None); log4net.Config.XmlConfigurator.Configure(new FileInfo(fileFullName)); } else { config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); log4net.Config.XmlConfigurator.Configure(); } var srvConfig = (ServiceStarterSection)config.GetSection("serviceStarters"); ServiceContext.Current.Configuration = srvConfig; ServiceContext.Current.IsDebug = param.IsDebug; PrepareAppender(srvConfig.ServiceInfo.Name, param.IsDebug ? "DEBUG" : "INFO"); if (Environment.UserInteractive) { try { if (param.IsShowHelp) { ShowHelp(param.Options); (string.Join(",", extra.ToArray())).Info(); return; } else if (param.IsShowVersion) { Version(); } else { CStarterMonitor monitor = new CStarterMonitor(); if(StartDaemons(srvConfig)) { if(StartServiceProccess(srvConfig)) { monitor.StartMonitor(); "按任意键关闭程序".Info(); Console.ReadLine(); monitor.StopMonitor(); "程序正在退出,请不要关闭窗口".Info(); string.Format("需要停止 {0} 个服务", ServiceContext.Current.ServiceSlots.Count).Debug(); if (0 != ServiceContext.Current.ServiceSlots.Count) { ServiceSlot[] slots = new ServiceSlot[ServiceContext.Current.ServiceSlots.Count]; ServiceContext.Current.ServiceSlots.CopyTo(slots); foreach (ServiceSlot slot in slots) { string.Format("正在停止服务:{0}", slot.Name).Debug(); (new CStarterClient()).Stop(srvConfig.ServiceInfo.Name, slot.Name, slot.Signal); if (!ServiceContext.Current.WaitServiceStopping(10)) { if (!slot.WorkProcess.WaitForExit(10 * 1000)) { slot.WorkProcess.Kill(); } } ServiceSlot tSlot = ServiceContext.Current.ServiceSlots.FirstOrDefault(s => s.Name == slot.Name); if (null != tSlot) { ServiceContext.Current.ServiceSlots.Remove(tSlot); } } } "停止监听服务".Debug(); CStarterDControlServiceDaemon.Current.Stop(); CStarterDNotifierServiceDaemon.Current.Stop(); "服务完全停止".Info(); Environment.Exit(0); } else { CStarterDControlServiceDaemon.Current.Stop(); CStarterDNotifierServiceDaemon.Current.Stop(); Environment.Exit(-1); } } else { "监听服务启动失败,无法继续启动".Error(); Environment.Exit(-1); } } } catch (Exception eX) { "servicestarter:".Error(); eX.Message.Error(); eX.Exception(); "使用命令cstarterd --help获取更多命令帮助".Info(); } } else { var srv = new WindowsService() { ServiceName = srvConfig.ServiceInfo.DisplayName }; srv.Initialize(srvConfig); ServiceBase.Run(srv); } }
static void Main(string[] args) { ServiceStarterParams param = new ServiceStarterParams(); List <string> extra = param.Parse(args); extra = param.Parse(args); Configuration config; if (!string.IsNullOrEmpty(param.ConfigurationFileName)) { string fileFullName = param.ConfigurationFileName; if (!Path.IsPathRooted(fileFullName)) { fileFullName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, param.ConfigurationFileName); } ExeConfigurationFileMap map = new ExeConfigurationFileMap(); map.ExeConfigFilename = fileFullName; config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None); log4net.Config.XmlConfigurator.Configure(new FileInfo(fileFullName)); } else { config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); log4net.Config.XmlConfigurator.Configure(); } var srvConfig = (ServiceStarterSection)config.GetSection("serviceStarters"); ServiceContext.Current.Configuration = srvConfig; ServiceContext.Current.IsDebug = param.IsDebug; PrepareAppender(srvConfig.ServiceInfo.Name, param.IsDebug ? "DEBUG" : "INFO"); if (Environment.UserInteractive) { try { if (param.IsShowHelp) { ShowHelp(param.Options); (string.Join(",", extra.ToArray())).Info(); return; } else if (param.IsShowVersion) { Version(); } else { CStarterMonitor monitor = new CStarterMonitor(); if (StartDaemons(srvConfig)) { if (StartServiceProccess(srvConfig)) { monitor.StartMonitor(); "按任意键关闭程序".Info(); Console.ReadLine(); monitor.StopMonitor(); "程序正在退出,请不要关闭窗口".Info(); string.Format("需要停止 {0} 个服务", ServiceContext.Current.ServiceSlots.Count).Debug(); if (0 != ServiceContext.Current.ServiceSlots.Count) { ServiceSlot[] slots = new ServiceSlot[ServiceContext.Current.ServiceSlots.Count]; ServiceContext.Current.ServiceSlots.CopyTo(slots); foreach (ServiceSlot slot in slots) { string.Format("正在停止服务:{0}", slot.Name).Debug(); (new CStarterClient()).Stop(srvConfig.ServiceInfo.Name, slot.Name, slot.Signal); if (!ServiceContext.Current.WaitServiceStopping(10)) { if (!slot.WorkProcess.WaitForExit(10 * 1000)) { slot.WorkProcess.Kill(); } } ServiceSlot tSlot = ServiceContext.Current.ServiceSlots.FirstOrDefault(s => s.Name == slot.Name); if (null != tSlot) { ServiceContext.Current.ServiceSlots.Remove(tSlot); } } } "停止监听服务".Debug(); CStarterDControlServiceDaemon.Current.Stop(); CStarterDNotifierServiceDaemon.Current.Stop(); "服务完全停止".Info(); Environment.Exit(0); } else { CStarterDControlServiceDaemon.Current.Stop(); CStarterDNotifierServiceDaemon.Current.Stop(); Environment.Exit(-1); } } else { "监听服务启动失败,无法继续启动".Error(); Environment.Exit(-1); } } } catch (Exception eX) { "servicestarter:".Error(); eX.Message.Error(); eX.Exception(); "使用命令cstarterd --help获取更多命令帮助".Info(); } } else { var srv = new WindowsService() { ServiceName = srvConfig.ServiceInfo.DisplayName }; srv.Initialize(srvConfig); ServiceBase.Run(srv); } }