Beispiel #1
0
        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;

            PrepareLogger(srvConfig.ServiceInfo.Name);

            if (Environment.UserInteractive)
            {
                try
                {
                    if (param.IsShowHelp)
                    {
                        ShowHelp(param.Options);
                        (string.Join(",", extra.ToArray())).Info();
                        return;
                    }
                    else if (param.IsShowVersion)
                    {
                        Version();
                    }
                    else
                    {
                        "启动监听服务".Info();

                        CStarterDControlServiceDaemon.Current.Start(srvConfig);
                        CStarterDNotifierServiceDaemon.Current.Start(srvConfig);

                        "监听服务已经启动".Info();

                        BasicServiceStarter.Run(srvConfig);

                        "按任意键关闭程序".Info();
                        Console.ReadLine();

                        "程序正在退出,请不要关闭窗口".Info();
                        string.Format("需要停止 {0} 个服务", ServiceContext.Current.ServiceSlots.Count).Info();

                        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).Info();

                                EventWaitHandle waitExitSignal;
                                bool created = EventWaitHandleHelper.Create("exit_" + slot.Signal, EventResetMode.ManualReset, out waitExitSignal);

                                (new CStarterClient()).Stop(srvConfig.ServiceInfo.Name, slot.Name, slot.Signal);

                                if (waitExitSignal.WaitOne(10 * 1000))
                                {
                                    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);
                                        }
                                    }
                                }
                            }
                        }

                        "停止监听服务".Info();
                        CStarterDControlServiceDaemon.Current.Stop();
                        CStarterDNotifierServiceDaemon.Current.Stop();
                    }
                }
                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);
            }
        }
Beispiel #2
0
        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;

            PrepareLogger(srvConfig.ServiceInfo.Name);

            if (Environment.UserInteractive)
            {
                try
                {
                    if (param.IsShowHelp)
                    {
                        ShowHelp(param.Options);
                        (string.Join(",", extra.ToArray())).Info();
                        return;
                    }
                    else if (param.IsShowVersion)
                    {
                        Version();
                    }
                    else
                    {
                        "启动监听服务".Info();

                        CStarterDControlServiceDaemon.Current.Start(srvConfig);
                        CStarterDNotifierServiceDaemon.Current.Start(srvConfig);

                        "监听服务已经启动".Info();

                        BasicServiceStarter.Run(srvConfig);

                        "按任意键关闭程序".Info();
                        Console.ReadLine();

                        "程序正在退出,请不要关闭窗口".Info();
                        string.Format("需要停止 {0} 个服务", ServiceContext.Current.ServiceSlots.Count).Info();

                        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).Info();

                                EventWaitHandle waitExitSignal;
                                bool            created = EventWaitHandleHelper.Create("exit_" + slot.Signal, EventResetMode.ManualReset, out waitExitSignal);

                                (new CStarterClient()).Stop(srvConfig.ServiceInfo.Name, slot.Name, slot.Signal);

                                if (waitExitSignal.WaitOne(10 * 1000))
                                {
                                    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);
                                        }
                                    }
                                }
                            }
                        }

                        "停止监听服务".Info();
                        CStarterDControlServiceDaemon.Current.Stop();
                        CStarterDNotifierServiceDaemon.Current.Stop();
                    }
                }
                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);
            }
        }