Ejemplo n.º 1
0
 /// <summary>
 /// 构造函数。
 /// </summary>
 public ServiceMain(WinServiceLog servlog)
     : base()
 {
     try
     {
         this.ServiceLog = servlog;
         this.Runing = false;
         //服务名称。
         this.ServiceName = ServiceConfig.ModuleConfig.ServiceName;
         //初始化主定时器.
         this.timer = new System.Timers.Timer(ModuleConst.CONST_DOU_TIMEINTERVAL);
         this.timer.Elapsed += this.ServiceTimerMain;
         this.timer.Enabled = false;
     }
     catch (Exception e)
     {
         servlog.ErrorLog(e);
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        /// <param name="args">命令行参数。</param>
        public static void Main(string[] args)
        {
            WinServiceLog servLog = null;
            try
            {
                servLog = new WinServiceLog(ServiceConfig.ModuleConfig);
                string svcName = ServiceConfig.ModuleConfig.ServiceName;
                bool startService = true;

                #region 安装与卸载服务.
                if (args != null && args.Length > 0)
                {
                    string assemblyName = Assembly.GetExecutingAssembly().ManifestModule.Name;
                    switch (args[0].ToLower())
                    {
                        case "-i":
                        case "-install":
                            {
                                #region 安装服务.
                                try
                                {
                                    servLog.ContentLog(string.Format("开始安装[{0}]服务...", svcName));
                                    if (ServiceIsExisted(svcName))
                                    {
                                        servLog.ContentLog(string.Format("服务[{0}]已经存在,无需重复安装。", svcName));
                                        break;
                                    }
                                    ManagedInstallerClass.InstallHelper(new string[] { assemblyName });
                                    ServiceController sc = new ServiceController(svcName);
                                    sc.Start();
                                    servLog.ContentLog(string.Format("安装服务[{0}]成功。", svcName));

                                    startService = true;
                                }
                                catch (Exception e)
                                {
                                    startService = false;
                                    servLog.ContentLog(string.Format("安装[{0}]服务失败[{1}]。", svcName, e.Message));
                                    servLog.ErrorLog(e);
                                }
                                #endregion
                            }
                            break;
                        case "-u":
                        case "-uninstall":
                            {
                                #region 卸载服务.
                                try
                                {
                                    if (ServiceIsExisted(svcName))
                                    {
                                        servLog.ContentLog(string.Format("开始卸载{0}服务...", svcName));
                                        ManagedInstallerClass.InstallHelper(new string[] { "/u", assemblyName });
                                        servLog.ContentLog(string.Format("卸载[{0}]服务成功。", svcName));
                                    }
                                    else
                                    {
                                        servLog.ContentLog(string.Format("服务{0}已经卸载。", svcName));
                                    }

                                }
                                catch (Exception e)
                                {
                                    servLog.ContentLog(string.Format("卸载[{0}]服务失败[{1}]。", svcName, e.Message));
                                    servLog.ErrorLog(e);
                                }
                                finally
                                {
                                    startService = false;
                                }
                                #endregion
                            }
                            break;
                        default:
                            {
                                servLog.ContentLog(string.Format("命令 {0} 不存在。", args));
                                startService = false;
                            }
                            break;
                    }
                }
                #endregion

                //运行服务.
                if (startService)
                {
                    servLog.ContentLog(string.Format("开始启动[{0}]服务...", svcName));
                    ServiceBase.Run(new ServiceBase[] { new ServiceMain(servLog) });
                    servLog.ContentLog(string.Format("[{0}]服务启动成功。", svcName));
                }
            }
            catch (Exception e)
            {
                if (servLog != null)
                {
                    servLog.ErrorLog(new Exception("Windows服务壳程序入口异常:" + e.Message, e));
                }
                else
                {
                    WriteExceptionLog(typeof(Program), new Exception("Windows服务壳程序入口异常:" + e.Message, e));
                }
            }
        }