Exemplo n.º 1
0
        /// <summary>
        /// 同步数据。
        /// </summary>
        /// <param name="log">服务日志对象。</param>
        public void Sync(WinServiceLog log)
        {
            try
            {
                if (this.DataPoxy == null)
                {
                    return;
                }

                this.logBuilder = new StringBuilder(this.SyncName);
                this.insertBuilder = new StringBuilder("新增" + this.SyncName + "数据:");
                this.updateBuilder = new StringBuilder("更新" + this.SyncName + "数据:");
                this.hiddenBuilder = new StringBuilder("隐藏" + this.SyncName + "数据:");
                this.insertCount = this.updateCount = this.hiddenCount = 0;

                this.DataSync(this.logBuilder, this.insertBuilder, this.updateBuilder, this.hiddenBuilder,
                    this.insertCount, this.updateCount, this.hiddenCount);
            }
            catch (Exception e)
            {
                this.AppendLog(this.logBuilder, string.Format("发生异常:{0}\r\n{1}\r\n{2}", e.Message, e.Source, e.StackTrace));
                throw e;
            }
            finally
            {
                this.AppendLog(this.logBuilder, null);
                StringBuilder sb = new StringBuilder();
                if (insertCount > 0)
                {
                    this.AppendLog(this.logBuilder, this.insertBuilder.ToString());
                    sb.AppendFormat("共新增:{0};", insertCount);
                }
                if (updateCount > 0)
                {
                    this.AppendLog(this.logBuilder, this.updateBuilder.ToString());
                    sb.AppendFormat("共更新:{0};", updateCount);
                }
                if (hiddenCount > 0)
                {
                    this.AppendLog(this.logBuilder, this.hiddenBuilder.ToString());
                    sb.AppendFormat("共隐藏:{0};", hiddenCount);
                }
                if (sb.Length > 0)
                {
                    this.AppendLog(this.logBuilder, sb.ToString());
                }
                if (log != null)
                {
                    log.ContentLog(this.logBuilder.ToString());
                }
            }
        }
Exemplo 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));
                }
            }
        }