Пример #1
0
        /// <summary>服务主函数</summary>
        public void Main()
        {
            MachineInfo.RegisterAsync();

            // 以服务方式启动时,不写控制台日志
            var args = Environment.GetCommandLineArgs();

            if (args == null || args.Length <= 1 || args[1].ToLower() != "-s")
            {
                XTrace.UseConsole();
            }

            if (Host == null)
            {
                if (Runtime.Windows)
                {
                    Host = new WindowsService();
                }
                else
                {
                    Host = new Systemd();
                }
            }

            var service = this;

            service.Log = XTrace.Log;

            // 初始化配置
            var set = Setting.Current;

            if (set.ServiceName.IsNullOrEmpty())
            {
                set.ServiceName = service.ServiceName;
            }
            if (set.DisplayName.IsNullOrEmpty())
            {
                set.DisplayName = service.DisplayName;
            }
            if (set.Description.IsNullOrEmpty())
            {
                set.Description = service.Description;
            }

            // 从程序集构造配置
            var asm = AssemblyX.Entry;

            if (set.ServiceName.IsNullOrEmpty())
            {
                set.ServiceName = asm.Name;
            }
            if (set.DisplayName.IsNullOrEmpty())
            {
                set.DisplayName = asm.Title;
            }
            if (set.Description.IsNullOrEmpty())
            {
                set.Description = asm.Description;
            }

            set.Save();

            // 用配置覆盖
            service.ServiceName = set.ServiceName;
            service.DisplayName = set.DisplayName;
            service.Description = set.Description;

            if (args.Length > 1)
            {
                #region 命令
                var cmd = args[1].ToLower();
                switch (cmd)
                {
                case "-s":
                    Host.Run(this);
                    break;

                case "-i":
                    Install();
                    break;

                case "-u":
                    Host.Remove(ServiceName);
                    break;

                case "-start":
                    Host.Start(ServiceName);
                    break;

                case "-stop":
                    Host.Stop(ServiceName);
                    break;
                }
                #endregion
            }
            else
            {
                Console.Title = service.DisplayName;

                // 输出状态,菜单循环
                service.ShowStatus();
                service.ProcessMenu();
            }
        }
Пример #2
0
        /// <summary>服务主函数</summary>
        /// <param name="args"></param>
        public void Main(String[] args)
        {
            if (args == null)
            {
                args = Environment.GetCommandLineArgs();
            }

            if (Host == null)
            {
                if (Runtime.Windows)
                {
                    Host = new WindowsService();
                }
                else
                {
                    Host = new Systemd();
                }
            }

            var service = this;

            service.Log = XTrace.Log;

            // 初始化配置
            var set = Setting.Current;

            if (set.ServiceName.IsNullOrEmpty())
            {
                set.ServiceName = service.ServiceName;
            }
            if (set.DisplayName.IsNullOrEmpty())
            {
                set.DisplayName = service.DisplayName;
            }
            if (set.Description.IsNullOrEmpty())
            {
                set.Description = service.Description;
            }

            // 从程序集构造配置
            var asm = AssemblyX.Entry;

            if (set.ServiceName.IsNullOrEmpty())
            {
                set.ServiceName = asm.Name;
            }
            if (set.DisplayName.IsNullOrEmpty())
            {
                set.DisplayName = asm.Title;
            }
            if (set.Description.IsNullOrEmpty())
            {
                set.Description = asm.Description;
            }

            // 用配置覆盖
            service.ServiceName = set.ServiceName;
            service.DisplayName = set.DisplayName;
            service.Description = set.Description;

            set.Save();

            if (args.Length > 0)
            {
                #region 命令
                var name = ServiceName;
                var cmd  = args[0].ToLower();
                switch (cmd)
                {
                case "-s":
                    Host.Run(this);
                    break;

                case "-i":
                    Install();
                    break;

                case "-u":
                    Host.Remove(name);
                    break;

                case "-start":
                    Host.Start(name);
                    break;

                case "-stop":
                    Host.Stop(name);
                    break;

                case "-restart":
                    Host.Restart(name);
                    break;

                case "-run":
                    StartLoop();
                    DoLoop();
                    StopLoop();
                    break;

                default:
                    // 快速调用自定义菜单
                    if (cmd.Length == 2 && cmd[0] == '-' && _Menus.TryGetValue(cmd[1], out var menu))
                    {
                        menu.Callback();
                    }
                    break;
                }
                #endregion

                ProcessCommand(args);
            }
            else
            {
                if (!service.DisplayName.IsNullOrEmpty())
                {
                    Console.Title = service.DisplayName;
                }

                // 输出状态,菜单循环
                service.ShowStatus();
                service.ProcessMenu();
            }

            if (XTrace.Log is CompositeLog compositeLog)
            {
                var log = compositeLog.Get <TextFileLog>();
                log.TryDispose();
            }
        }
Пример #3
0
        /// <summary>服务主函数</summary>
        /// <param name="args"></param>
        public void Main(String[] args)
        {
            //#if NETSTANDARD2_0
            //MachineInfo.RegisterAsync();
            //#endif

            // 以服务方式启动时,不写控制台日志
            if (args == null)
            {
                args = Environment.GetCommandLineArgs();
            }
            var isService = args != null && args.Length > 0 || args.Contains("-s");

            if (!isService)
            {
                XTrace.UseConsole();
            }

            if (Host == null)
            {
                if (Runtime.Windows)
                {
                    Host = new WindowsService();
                }
                else
                {
                    Host = new Systemd();
                }
            }

            var service = this;

            service.Log = XTrace.Log;

            // 初始化配置
            var set = Setting.Current;

            if (set.ServiceName.IsNullOrEmpty())
            {
                set.ServiceName = service.ServiceName;
            }
            if (set.DisplayName.IsNullOrEmpty())
            {
                set.DisplayName = service.DisplayName;
            }
            if (set.Description.IsNullOrEmpty())
            {
                set.Description = service.Description;
            }

            // 从程序集构造配置
            var asm = AssemblyX.Entry;

            if (set.ServiceName.IsNullOrEmpty())
            {
                set.ServiceName = asm.Name;
            }
            if (set.DisplayName.IsNullOrEmpty())
            {
                set.DisplayName = asm.Title;
            }
            if (set.Description.IsNullOrEmpty())
            {
                set.Description = asm.Description;
            }

            set.Save();

            // 用配置覆盖
            service.ServiceName = set.ServiceName;
            service.DisplayName = set.DisplayName;
            service.Description = set.Description;

            if (args.Length > 0)
            {
                #region 命令
                var name = ServiceName;
                var cmd  = args[0].ToLower();
                switch (cmd)
                {
                case "-s":
                    Host.Run(this);
                    break;

                case "-i":
                    Install();
                    break;

                case "-u":
                    Host.Remove(name);
                    break;

                case "-start":
                    Host.Start(name);
                    break;

                case "-stop":
                    Host.Stop(name);
                    break;

                case "-restart":
                    Host.Restart(name);
                    break;
                }
                #endregion
            }
            else
            {
                Console.Title = service.DisplayName;

                // 输出状态,菜单循环
                service.ShowStatus();
                service.ProcessMenu();
            }

            if (XTrace.Log is CompositeLog compositeLog)
            {
                var log = compositeLog.Get <TextFileLog>();
                log.TryDispose();
            }
        }