示例#1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MinaServiceBase"/> class.
        /// </summary>
        /// <param name="service">The service.</param>
        /// <param name="option">The option.</param>
        public MinaServiceBase(MinaService service, MinaServiceOption option) : this()
        {
            this.service = service;
            this.options = option;

            SetOptions();
        }
示例#2
0
        /// <summary>
        /// Runs the service with specified option.
        /// </summary>
        /// <param name="option">The option.</param>
        /// <param name="service">The service.</param>
        /// <param name="args">The arguments.</param>
        /// <param name="installer">The installer.</param>
        /// <exception cref="ArgumentNullException">option</exception>
        public static void Run(Action <MinaOption> option, MinaService service, string[] args = null, MinaInstaller installer = null)
        {
            try
            {
                if (option == null)
                {
                    throw new ArgumentNullException(nameof(option));
                }

                MinaOption _option = new MinaOption();

                option.Invoke(_option);

                Run(_option, service, args, installer);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
示例#3
0
 /// <summary>
 /// Sets the service.
 /// </summary>
 /// <param name="service">The service.</param>
 public void SetService(MinaService service)
 {
     this.service = service;
 }
示例#4
0
        /// <summary>
        /// Runs the service with specified option.
        /// </summary>
        /// <param name="option">The option.</param>
        /// <param name="service">The service.</param>
        /// <param name="args">The arguments.</param>
        /// <param name="installer">The installer.</param>
        /// <exception cref="ArgumentNullException">
        /// option
        /// or
        /// service
        /// </exception>
        /// <exception cref="InvalidOperationException"></exception>
        public static void Run(MinaOption option, MinaService service, string[] args = null, MinaInstaller installer = null)
        {
            try
            {
                // parameter check
                if (option == null)
                {
                    throw new ArgumentNullException(nameof(option));
                }
                if (service == null)
                {
                    throw new ArgumentNullException(nameof(service));
                }

                if (Extension.IsNullOrWhiteSpace(option.Service.ServiceName))
                {
                    throw new ArgumentNullException(nameof(option.Service.ServiceName));
                }

                if (Extension.IsNullOrWhiteSpace(option.Service.ServiceName))
                {
                    option.Service.DisplayName = option.Service.ServiceName;
                }

                // service initialize
                service.OnInitialize();

                bool help      = false;
                bool install   = false;
                bool uninstall = false;
                bool start     = false;
                bool stop      = false;
                bool restart   = false;
                bool pause     = false;
                bool continuee = false;
                bool command   = false;
                int  cmd       = -1;

                string parameter = null;

                if (args?.Length > 0)
                {
                    int    length = args.Length;
                    string action = args[0];

                    help = action.Equals("-h") || action.Equals("/h") || action.Equals("-help") || action.Equals("/help");

                    install = action.Equals("-i") || action.Equals("/i") || action.Equals("-install") || action.Equals("/install");

                    if (install && length > 1)
                    {
                        for (int i = 1; i < length; i++)
                        {
                            if (!Extension.IsNullOrWhiteSpace(args[i]))
                            {
                                parameter += $"{args[i]} ";
                            }
                        }

                        parameter = parameter.Trim();
                    }

                    uninstall = action.Equals("-u") || action.Equals("/u") || action.Equals("-uninstall") || action.Equals("/uninstall");

                    start   = action.Equals("-start") || action.Equals("/start");
                    stop    = action.Equals("-stop") || action.Equals("/stop");
                    restart = action.Equals("-r") || action.Equals("/r") || action.Equals("-restart") || action.Equals("/restart");

                    pause     = action.Equals("-p") || action.Equals("/p") || action.Equals("-pause") || action.Equals("/pause");
                    continuee = action.Equals("-c") || action.Equals("/c") || action.Equals("-continue") || action.Equals("/continue");

                    command = action.Equals("-cmd") || action.Equals("/cmd") || action.Equals("-commnd") || action.Equals("/commnd");

                    if (command && length > 1)
                    {
                        bool ret = int.TryParse(args[1], out cmd);
                        command = ret && cmd != -1;

                        if (!command)
                        {
                            return;
                        }
                    }
                }

                if (help)
                {
                    PrintHelp();
                    return;
                }

                if (start)
                {
                    ExecuteActionCheckServiceExist(option.Service.ServiceName, () => ServiceControllerHelper.Start(option.Service.ServiceName));
                    return;
                }

                if (stop)
                {
                    ExecuteActionCheckServiceExist(option.Service.ServiceName, () => ServiceControllerHelper.Stop(option.Service.ServiceName));
                    return;
                }

                if (restart)
                {
                    ExecuteActionCheckServiceExist(option.Service.ServiceName, () => ServiceControllerHelper.ReStart(option.Service.ServiceName));
                    return;
                }

                if (pause)
                {
                    ExecuteActionCheckServiceExist(option.Service.ServiceName, () => ServiceControllerHelper.Pause(option.Service.ServiceName));
                    return;
                }

                if (continuee)
                {
                    ExecuteActionCheckServiceExist(option.Service.ServiceName, () => ServiceControllerHelper.Continue(option.Service.ServiceName));
                    return;
                }

                if (command)
                {
                    ServiceControllerHelper.Command(option.Service.ServiceName, cmd);
                    return;
                }

                // initialize installer
                MinaServiceInstaller msi = null;

                if (install || uninstall)
                {
                    msi = new MinaServiceInstaller(option);
                }

                // install service
                if (install)
                {
                    installer?.OnBeforeInstall();

                    msi.Install(parameter);
                    msi.Dispose();

                    installer?.OnAfterInstall();

                    if (option.RunServiceAfterInstall)
                    {
                        ServiceControllerHelper.Start(option.Service.ServiceName);
                    }

                    return;
                }

                // uninstall service
                if (uninstall)
                {
                    installer?.OnBeforeUninstall();

                    msi.Uninstall();
                    msi.Dispose();

                    installer?.OnAfterUninstall();
                    return;
                }

                ExecuteActionCheckServiceExist(option.Service.ServiceName, null);

                // run service
                ServiceBase[] ServicesToRun;

                ServicesToRun = new ServiceBase[]
                {
                    new MinaServiceBase(service, option.Service)
                };

                ServiceBase.Run(ServicesToRun);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }