/// <summary> /// Starts the service as service process or user interactive commandline program. In user interactive mode a logconsole is created to receive messages. /// In service mode an eventlog is used. /// </summary> public void Run() { try { Init(); if (IsWindowsService) { Run(this); return; } if (SystemConsole.IsConsoleAvailable && SystemConsole.CanReadKey) { SystemConsole.SetKeyPressedEvent(OnKeyPressed); } // run commandline CommandLineRun(); } catch (Exception ex) { log.LogEmergency(ex, "Unhandled exception:\n" + ex.ToXT()); } finally { // force stop if not already stopped / set stopped flag at service if (IsWindowsService) { Stop(); } Logger.Flush(); Logger.Close(); SystemConsole.RemoveKeyPressedEvent(); if (Debugger.IsAttached) { Thread.Sleep(1000); SystemConsole.WriteLine("--- Press <yellow>enter<default> to exit ---"); SystemConsole.ReadLine(); } } }
/// <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) { } } }