コード例 #1
0
        /// <summary>Handles the service start event creating a background thread calling the <see cref="RunWorker"/> function.</summary>
        /// <param name="args">The arguments.</param>
        protected override void OnStart(string[] args)
        {
            if (ServiceParameters != null)
            {
                return;
            }

            log.LogInfo("Starting service <cyan>{0}<default>...", ServiceName);
            base.OnStart(args);
            ServiceParameters = new ServiceParameters(HasAdminRights, false, false);
            task = Task.Factory.StartNew(() =>
            {
                RunWorker();
                base.OnStop();
            }, TaskCreationOptions.LongRunning);
            log.LogInfo("Service <cyan>{0}<default> started.", ServiceName);
        }
コード例 #2
0
 /// <summary>Called when [key pressed].</summary>
 /// <param name="keyInfo">The key information.</param>
 protected internal virtual void OnKeyPressed(ConsoleKeyInfo keyInfo)
 {
     if (keyInfo.Key == ConsoleKey.Escape)
     {
         var now  = DateTime.Now;
         var time = now - onKeyPressedEscape;
         onKeyPressedEscape = now;
         if (time > TimeSpan.Zero && time < OnKeyPressedEscapeShutdownTimeSpan)
         {
             ServiceParameters.CommitShutdown();
         }
         else
         {
             log.LogInfo("Press escape within <cyan>{0}<default> to perform shutdown.", OnKeyPressedEscapeShutdownTimeSpan.FormatTime());
         }
     }
 }
コード例 #3
0
        /// <summary>Tells the worker to shutdown by setting the <see cref="ServiceParameters"/>.</summary>
        protected override void OnStop()
        {
            if (ServiceParameters == null)
            {
                return;
            }

            log.LogInfo("Stopping service <cyan>{0}<default>...", ServiceName);
            ServiceParameters.IsStopping = true;
            if (!ServiceParameters.CommandLineMode)
            {
                base.OnStop();
            }

            ServiceParameters.CommitShutdown();
            if (task != null)
            {
                task.Wait();
                task = null;
            }
            ServiceParameters = null;
            log.LogInfo("Service <cyan>{0}<default> stopped.", ServiceName);
        }
コード例 #4
0
        /// <summary>Does a commandline run.</summary>
        void CommandLineRun()
        {
            log.LogInfo("Initializing service <cyan>{0}<default> commandline instance...\nRelease: <magenta>{1}<default>, FileVersion: <magenta>{2}<default>", ServiceName, VersionInfo.AssemblyVersion, VersionInfo.FileVersion);
            var needAdminRights = false;

            if (Platform.IsMicrosoft)
            {
                foreach (var option in CommandlineArguments.Options)
                {
                    switch (option.Name)
                    {
                    case "start":
                    case "stop":
                    case "install":
                    case "uninstall": needAdminRights = true; break;
                    }
                }

                if (needAdminRights && !HasAdminRights)
                {
                    if (Debugger.IsAttached)
                    {
                        throw new InvalidOperationException("Please debug this program in administration mode!");
                    }

                    log.LogNotice("Restarting service with administration rights!");
                    Logger.Close();
                    var processStartInfo = new ProcessStartInfo(CommandlineArguments.Command, CommandlineArguments.ToString(false) + " --wait")
                    {
                        UseShellExecute = true,
                        Verb            = "runas",
                    };
                    Process.Start(processStartInfo);
                    return;
                }
                if (HasAdminRights)
                {
                    log.LogInfo("Current user has <green>admin<default> rights.");
                }
                else
                {
                    log.LogInfo("Running in <red>debug<default> mode <red>without admin<default> rights.");
                }
            }

            bool runCommandLine;
            var  wait = CommandlineArguments.IsOptionPresent("wait");

            if (Debugger.IsAttached)
            {
                runCommandLine = true;
                log.LogInfo("<red>Debugger<default> attached.");
            }
            else
            {
                if (CommandlineArguments.Options.Count == 0)
                {
                    Help();
                    return;
                }
                runCommandLine = CommandlineArguments.IsOptionPresent("run");
            }
            bool isInteractive;

            if (Platform.IsMicrosoft)
            {
                ServiceHelper.ServiceName = ServiceName;
                isInteractive             = Environment.UserInteractive;
                if (CommandlineArguments.IsHelpOptionFound())
                {
                    Help();
                    return;
                }
                foreach (var option in CommandlineArguments.Options)
                {
                    switch (option.Name)
                    {
                    case "start":
                        if (Debugger.IsAttached || !runCommandLine)
                        {
                            ServiceHelper.StartService();
                        }
                        else
                        {
                            log.LogError("Ignore <red>start<default> service command, doing commandline run!");
                        }

                        break;

                    case "stop":
                        if (Debugger.IsAttached || !runCommandLine)
                        {
                            ServiceHelper.StopService();
                        }
                        else
                        {
                            log.LogError("Ignore <red>stop<default> service command, doing commandline run!");
                        }

                        break;

                    case "install":
                        if (Debugger.IsAttached || !runCommandLine)
                        {
                            ServiceHelper.InstallService();
                        }
                        else
                        {
                            log.LogError("Ignore <red>install<default> service command, doing commandline run!");
                        }

                        break;

                    case "uninstall":
                        if (Debugger.IsAttached || !runCommandLine)
                        {
                            ServiceHelper.UnInstallService();
                        }
                        else
                        {
                            log.LogError("Ignore <red>uninstall<default> service command, doing commandline run!");
                        }

                        break;
                    }
                }
            }
            else
            {
                isInteractive  = !CommandlineArguments.IsOptionPresent("daemon");
                runCommandLine = true;
            }
            if (runCommandLine)
            {
                // --- start service as program
                ServiceParameters = new ServiceParameters(HasAdminRights, true, isInteractive);
                try
                {
                    RunWorker();
                }
                catch (Exception ex)
                {
                    log.LogError("Error while running service executable in commandline mode.\n" + ex.ToXT());
                }

                // --- exit
            }
            Logger.Flush();
            if (isInteractive && wait)
            {
                SystemConsole.RemoveKeyPressedEvent();
                while (SystemConsole.KeyAvailable)
                {
                    SystemConsole.ReadKey();
                }

                SystemConsole.WriteLine("--- Press <yellow>enter<default> to exit... ---");
                while (SystemConsole.ReadKey().Key != ConsoleKey.Enter)
                {
                }
            }
        }