public void Start(ServiceStarterSection config) { _SrvConfig = config; try { "初始化监听服务对象".Debug(); _Srv = new CStarterDController(); "初始化监听服务配置".Debug(); string.Format("监听服务监听地址:{0}", "net.pipe://localhost/" + _SrvConfig.ServiceInfo.Name).Debug(); _Host = new ServiceHost(_Srv, new Uri("net.pipe://localhost/" + _SrvConfig.ServiceInfo.Name)); _Host.AddServiceEndpoint(typeof(ICStarterDControl), new NetNamedPipeBinding(), "control"); "开启监听服务".Debug(); _Host.Open(); _IsStarted = true; } catch (Exception eX) { eX.Message.Error(); eX.Exception(); } }
public void StartService(ServiceStarterSection srvConfig, string name) { EndpointAddress ep = new EndpointAddress(string.Format("net.pipe://localhost/{0}/{1}", srvConfig.ServiceInfo.Name, "control")); ActionResult result = null; ExecuteNamedPipeAction(ep, (channel) => { result = channel.StartService(name); }, 3, (msg) => { result.Result = -1; result.Message = msg; }); if (0 == result.Result) { "服务 {0} 成功启动".Formate(name).Info(); } else { "服务 {0} 启动失败:{1}".Formate(name, result.Message).Error(); } }
public string GetServiceInfo(ServiceStarterSection srvConfig, string name) { EndpointAddress ep = new EndpointAddress(string.Format("net.pipe://localhost/{0}/{1}", srvConfig.ServiceInfo.Name, "control")); string retValue = ""; ActionResult result = null; ExecuteNamedPipeAction(ep, (channel) => { result = channel.GetServiceInfo(name); }, 3, (msg) => { result.Result = -1; result.Message = msg; }); if (0 == result.Result) { retValue = result.Data as string; } else { result.Message.Error(); } return(retValue); }
public void StopService(ServiceStarterSection srvConfig, string name) { EndpointAddress ep = new EndpointAddress(string.Format("net.pipe://localhost/{0}/{1}", srvConfig.ServiceInfo.Name, "control")); ActionResult result = null; "等待服务 {0} 停止".Formate(name).Info(); ExecuteNamedPipeAction(ep, (channel) => { result = channel.StopService(name, 10); }, 3, (msg) => { result.Result = -1; result.Message = msg; }); if (0 == result.Result) { ExecuteNamedPipeAction(ep, (channel) => { ActionResult chkResult = channel.CheckService(name); if (-1 == chkResult.Result) { chkResult.Message.Error(); } else { if (0 == chkResult.Result) { "服务 {0} 成功停止".Formate(name).Info(); } else { "服务 {0} 停止失败".Formate(name).Error(); } } }, 3, (msg) => { msg.Error(); }); } else { result.Message.Error(); } }
static bool StartServiceProccess(ServiceStarterSection srvConfig) { bool retValue = false; try { "启动注册的服务".Info(); BasicServiceStarter.Run(srvConfig); "服务都已经启动".Info(); retValue = true; } catch (Exception eX) { retValue = false; } return(retValue); }
static bool StartDaemons(ServiceStarterSection srvConfig) { bool retValue = false; try { "启动监听服务".Debug(); CStarterDControlServiceDaemon.Current.Start(srvConfig); CStarterDNotifierServiceDaemon.Current.Start(srvConfig); "监听服务已经启动".Debug(); retValue = true; } catch (Exception eX) { eX.Exception(); } return(retValue); }
static void Main(string[] args) { bool isShowHelp = false; bool isInstall = false; bool isUninstall = false; bool isStop = false; bool isRun = false; bool isShowVersion = false; bool isListServices = false; bool isShowServiceInfo = false; string restartName = string.Empty; string targetService = string.Empty; var p = new OptionSet(){ { "i|install", "安装服务", v => isInstall = true}, { "u|uninstall", "卸载服务", v => isUninstall = true}, { "v|version", "显示版本号", v => isShowVersion = true}, { "o|stop=", "停止指定的服务", v => { isStop = true; restartName = v; } }, { "r|run=", "运行指定的服务", v => { isRun = true; restartName = v; } }, { "l|list", "列出服务列表", v => isListServices = true }, { "n|info=", "列出服务信息", v => { targetService = v; isShowServiceInfo = true; } }, { "s|service=", "运行指定名称的服务(该服务必须已经配置在配置文件中)", v => targetService = v }, { "c|config=", "要使用的配置文件,默认是CStarterD.exe.config,可以与i配合使用", v => _ConfigFile = v}, { "h|help", "显示帮助", v=>isShowHelp=true} }; List<string> extra; try { extra = p.Parse(args); if (string.IsNullOrEmpty(_ConfigFile)) { _ConfigFile = "CStarterD.exe.config"; } string fileFullName = _ConfigFile; if (!Path.IsPathRooted(fileFullName)) { fileFullName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, _ConfigFile); } ExeConfigurationFileMap map = new ExeConfigurationFileMap(); map.ExeConfigFilename = fileFullName; _Config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None); _SrcConfig = (ServiceStarterSection)_Config.GetSection("serviceStarters"); if (isShowHelp) { ShowHelp(p); (string.Join(",", extra.ToArray())).Info(); return; } else if (isShowVersion) { Version(); } else if (isInstall) { Install(); } else if (isUninstall) { UnInstall(); } else if (isListServices) { string names = (new CStarterDControlClient()).GetServices(_SrcConfig); names.Info(); } else if (isShowServiceInfo) { string info = (new CStarterDControlClient()).GetServiceInfo(_SrcConfig, targetService); info.Info(); } else if (isStop) { if (!string.IsNullOrEmpty(restartName)) { "确定要停止服务:{0}?(Y/N)".Formate(restartName).Info(); string c = Console.ReadLine(); if ("Y" == c.ToUpper()) { (new CStarterDControlClient()).StopService(_SrcConfig, restartName); } } } else if (isRun) { if (!string.IsNullOrEmpty(restartName)) { (new CStarterDControlClient()).StartService(_SrcConfig, restartName); } } else { ShowHelp(p); } } catch (OptionException eX) { "CStarterAdmin:".Error(); eX.Message.Error(); eX.Exception(); "使用命令CStarterAdmin --help获取更多命令帮助".Info(); } }
private static List <ServiceStarterElement> SortServices(ServiceStarterSection serviceInfo) { List <ServiceStarterElement> retValue = new List <ServiceStarterElement>(); List <RefCount> countList = new List <RefCount>(); foreach (ServiceStarterElement ele in serviceInfo.Services) { if ("true" == ele.Enabled.ToLower()) { RefCount found = countList.FirstOrDefault(c => c.Name == ele.Name); if (null == found) { found = new RefCount() { Name = ele.Name, Count = 0, Element = ele }; countList.Add(found); } else { if (null == found.Element) { found.Element = ele; } } if (0 != ele.DependenceServices.Count) { foreach (DependenceServiceConfigurationElement dEle in ele.DependenceServices) { RefCount refFound = countList.FirstOrDefault(c => c.Name == dEle.ServiceName); if (null != refFound) { refFound.Count++; } else { refFound = new RefCount() { Name = dEle.ServiceName, Count = 1, Element = null }; countList.Add(refFound); } } } } else { string.Format("位于 {0} 位置的 {1} 服务未配置成可启动", ele.ContentPath, ele.Name).Warn(); } } countList.Sort(new Comparison <RefCount>((left, right) => { return(right.Count.CompareTo(left.Count)); })); retValue.AddRange((from c in countList select c.Element).ToArray()); return(retValue); }
private static List<ServiceStarterElement> SortServices(ServiceStarterSection serviceInfo) { List<ServiceStarterElement> retValue = new List<ServiceStarterElement>(); List<RefCount> countList = new List<RefCount>(); foreach (ServiceStarterElement ele in serviceInfo.Services) { if ("true" == ele.Enabled.ToLower()) { RefCount found = countList.FirstOrDefault(c => c.Name == ele.Name); if (null == found) { found = new RefCount() { Name = ele.Name, Count = 0, Element = ele }; countList.Add(found); } else { if (null == found.Element) found.Element = ele; } if (0 != ele.DependenceServices.Count) { foreach (DependenceServiceConfigurationElement dEle in ele.DependenceServices) { RefCount refFound = countList.FirstOrDefault(c => c.Name == dEle.ServiceName); if (null != refFound) { refFound.Count++; } else { refFound = new RefCount() { Name = dEle.ServiceName, Count = 1, Element = null }; countList.Add(refFound); } } } } else { string.Format("位于 {0} 位置的 {1} 服务未配置成可启动", ele.ContentPath, ele.Name).Warn(); } } countList.Sort(new Comparison<RefCount>((left, right) => { return right.Count.CompareTo(left.Count); })); retValue.AddRange((from c in countList select c.Element).ToArray()); return retValue; }
public static void Run(ServiceStarterSection serviceInfo) { if (null !=serviceInfo) { string.Format("服务正在使用 {0} 模式运行。", Environment.UserInteractive ? "交互" : "服务").Debug(); string.Format("共配置了:{0} 项服务", serviceInfo.Services.Count).Debug(); "解析服务启动顺序".Debug(); List<ServiceStarterElement> orderedConfigs = SortServices(serviceInfo); string.Format("服务按照如下顺序启动:{0}", string.Join("=>", (from n in orderedConfigs select n.Name).ToArray())).Debug(); try { foreach (ServiceStarterElement eleConfig in orderedConfigs) { string msg = ""; "启动服务 {0} 的进程".Formate(eleConfig.Name).Debug(); bool isContinued = true; if (0 != eleConfig.DependenceServices.Count) { string.Format("服务 {0} 依赖服务 {1}", eleConfig.Name, string.Join(",", (from refSrv in eleConfig.DependenceServices.Cast<DependenceServiceConfigurationElement>() select refSrv.ServiceName).ToArray())).Debug(); foreach(DependenceServiceConfigurationElement dependedOnSrv in eleConfig.DependenceServices) { "检测依赖服务 {0}".Formate(dependedOnSrv.ServiceName).Debug(); if (null == ServiceContext.Current.ServiceSlots.FirstOrDefault(s => s.Name == dependedOnSrv.ServiceName)) { "{0} 所依赖的服务 {1} 未启动,服务启动失败".Formate(eleConfig.Name, dependedOnSrv.ServiceName).Error(); isContinued = false; break; } } } if(isContinued) { if (!RunServiceProcess(serviceInfo.ServiceInfo.Name, eleConfig, out msg)) { msg.Error(); } } } "服务启动完毕".Debug(); } catch (Exception eX) { eX.Exception(); } } else { "找不到服务配置信息,启动失败。".Error(); } }
public void Start(ServiceStarterSection config) { _SrvConfig = config; try { "初始化监听服务对象".Debug(); _Srv = new CStarterDController(); "初始化监听服务配置".Debug(); string.Format("监听服务监听地址:{0}", "net.pipe://localhost/" + _SrvConfig.ServiceInfo.Name).Debug(); _Host = new ServiceHost(_Srv, new Uri("net.pipe://localhost/" + _SrvConfig.ServiceInfo.Name)); _Host.AddServiceEndpoint(typeof(ICStarterDControl), new NetNamedPipeBinding(), "control"); "开启监听服务".Debug(); _Host.Open(); _IsStarted = true; } catch(Exception eX) { eX.Message.Error(); eX.Exception(); } }
public void Start(ServiceStarterSection config) { _Controller.Start(config); }
public void Initialize(ServiceStarterSection srvConfig) { _Config = srvConfig; }
static void Main(string[] args) { bool isShowHelp = false; bool isInstall = false; bool isUninstall = false; bool isStop = false; bool isRun = false; bool isShowVersion = false; bool isListServices = false; bool isShowServiceInfo = false; string restartName = string.Empty; string targetService = string.Empty; var p = new OptionSet() { { "i|install", "安装服务", v => isInstall = true }, { "u|uninstall", "卸载服务", v => isUninstall = true }, { "v|version", "显示版本号", v => isShowVersion = true }, { "o|stop=", "停止指定的服务", v => { isStop = true; restartName = v; } }, { "r|run=", "运行指定的服务", v => { isRun = true; restartName = v; } }, { "l|list", "列出服务列表", v => isListServices = true }, { "n|info=", "列出服务信息", v => { targetService = v; isShowServiceInfo = true; } }, { "s|service=", "运行指定名称的服务(该服务必须已经配置在配置文件中)", v => targetService = v }, { "c|config=", "要使用的配置文件,默认是CStarterD.exe.config,可以与i配合使用", v => _ConfigFile = v }, { "h|help", "显示帮助", v => isShowHelp = true } }; List <string> extra; try { extra = p.Parse(args); if (string.IsNullOrEmpty(_ConfigFile)) { _ConfigFile = "CStarterD.exe.config"; } string fileFullName = _ConfigFile; if (!Path.IsPathRooted(fileFullName)) { fileFullName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, _ConfigFile); } ExeConfigurationFileMap map = new ExeConfigurationFileMap(); map.ExeConfigFilename = fileFullName; _Config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None); _SrcConfig = (ServiceStarterSection)_Config.GetSection("serviceStarters"); if (isShowHelp) { ShowHelp(p); (string.Join(",", extra.ToArray())).Info(); return; } else if (isShowVersion) { Version(); } else if (isInstall) { Install(); } else if (isUninstall) { UnInstall(); } else if (isListServices) { string names = (new CStarterDControlClient()).GetServices(_SrcConfig); names.Info(); } else if (isShowServiceInfo) { string info = (new CStarterDControlClient()).GetServiceInfo(_SrcConfig, targetService); info.Info(); } else if (isStop) { if (!string.IsNullOrEmpty(restartName)) { "确定要停止服务:{0}?(Y/N)".Formate(restartName).Info(); string c = Console.ReadLine(); if ("Y" == c.ToUpper()) { (new CStarterDControlClient()).StopService(_SrcConfig, restartName); } } } else if (isRun) { if (!string.IsNullOrEmpty(restartName)) { (new CStarterDControlClient()).StartService(_SrcConfig, restartName); } } else { ShowHelp(p); } } catch (OptionException eX) { "CStarterAdmin:".Error(); eX.Message.Error(); eX.Exception(); "使用命令CStarterAdmin --help获取更多命令帮助".Info(); } }
static bool StartDaemons(ServiceStarterSection srvConfig) { bool retValue = false; try { "启动监听服务".Debug(); CStarterDControlServiceDaemon.Current.Start(srvConfig); CStarterDNotifierServiceDaemon.Current.Start(srvConfig); "监听服务已经启动".Debug(); retValue = true; } catch(Exception eX) { eX.Exception(); } return retValue; }
public static void Run(ServiceStarterSection serviceInfo) { if (null != serviceInfo) { string.Format("服务正在使用 {0} 模式运行。", Environment.UserInteractive ? "交互" : "服务").Debug(); string.Format("共配置了:{0} 项服务", serviceInfo.Services.Count).Debug(); "解析服务启动顺序".Debug(); List <ServiceStarterElement> orderedConfigs = SortServices(serviceInfo); string.Format("服务按照如下顺序启动:{0}", string.Join("=>", (from n in orderedConfigs select n.Name).ToArray())).Debug(); try { foreach (ServiceStarterElement eleConfig in orderedConfigs) { string msg = ""; "启动服务 {0} 的进程".Formate(eleConfig.Name).Debug(); bool isContinued = true; if (0 != eleConfig.DependenceServices.Count) { string.Format("服务 {0} 依赖服务 {1}", eleConfig.Name, string.Join(",", (from refSrv in eleConfig.DependenceServices.Cast <DependenceServiceConfigurationElement>() select refSrv.ServiceName).ToArray())).Debug(); foreach (DependenceServiceConfigurationElement dependedOnSrv in eleConfig.DependenceServices) { "检测依赖服务 {0}".Formate(dependedOnSrv.ServiceName).Debug(); if (null == ServiceContext.Current.ServiceSlots.FirstOrDefault(s => s.Name == dependedOnSrv.ServiceName)) { "{0} 所依赖的服务 {1} 未启动,服务启动失败".Formate(eleConfig.Name, dependedOnSrv.ServiceName).Error(); isContinued = false; break; } } } if (isContinued) { if (!RunServiceProcess(serviceInfo.ServiceInfo.Name, eleConfig, out msg)) { msg.Error(); } } } "服务启动完毕".Debug(); } catch (Exception eX) { eX.Exception(); } } else { "找不到服务配置信息,启动失败。".Error(); } }
static bool StartServiceProccess(ServiceStarterSection srvConfig) { bool retValue = false; try { "启动注册的服务".Info(); BasicServiceStarter.Run(srvConfig); "服务都已经启动".Info(); retValue = true; } catch (Exception eX) { retValue = false; } return retValue; }