public void Install(IServiceConfiguration configuration) { Guard.AgainstNull(configuration, nameof(configuration)); GuardAdministrator(); if (!string.IsNullOrEmpty(configuration.ServicePath)) { new ServiceInvoker(configuration).Execute(ServiceCommand.Install); return; } CallContext.LogicalSetData(ServiceConfigurationKey, configuration); var instancedServiceName = configuration.GetInstancedServiceName(); var log = ServiceHostEventLog.GetEventLog(instancedServiceName); ConsoleExtensions.WriteLine(ConsoleColor.Green, $"Installing service '{instancedServiceName}'."); var entryAssembly = Assembly.GetEntryAssembly(); if (entryAssembly == null) { throw new InvalidOperationException("An entry assembly is required in order to install a service."); } var entryAssemblyLocation = entryAssembly.Location; if (string.IsNullOrEmpty(entryAssemblyLocation)) { throw new InvalidOperationException("The entry assembly has no location."); } if (!Path.GetExtension(entryAssemblyLocation).Equals(".exe", StringComparison.InvariantCultureIgnoreCase)) { throw new InvalidOperationException( "The entry assembly must be an 'exe' in order to install as a service."); } var assemblyInstaller = new AssemblyInstaller(typeof(ServiceHost).Assembly, null); using (var installer = assemblyInstaller) { IDictionary state = new Hashtable(); installer.UseNewContext = true; try { installer.Install(state); installer.Commit(state); var serviceKey = GetServiceKey(instancedServiceName); serviceKey.SetValue("Description", configuration.Description); serviceKey.SetValue("ImagePath", $"{entryAssemblyLocation} /serviceName=\"{configuration.ServiceName}\"{(string.IsNullOrEmpty(configuration.Instance) ? string.Empty : $" /instance=\"{configuration.Instance}\"")}"); }
public void Execute() { #if (!NETCOREAPP2_0 && !NETCOREAPP2_1 && !NETSTANDARD2_0) var serviceController = ServiceController.GetServices() .FirstOrDefault(s => s.ServiceName == _configuration.ServiceName); if (serviceController != null && serviceController.Status == ServiceControllerStatus.Running) { ConsoleExtensions.WriteLine(ConsoleColor.Yellow, $"WARNING: Windows service '{_configuration.ServiceName}' is running. The display name is '{serviceController.DisplayName}'."); Console.WriteLine(); } #endif var waitHandle = new ManualResetEvent(false); var waitHandles = new WaitHandle[] { waitHandle }; var stopping = false; Console.CancelKeyPress += (sender, e) => { if (stopping) { return; } ConsoleExtensions.WriteLine(ConsoleColor.Green, "[stopping]"); waitHandle.Set(); e.Cancel = true; stopping = true; }; _service.Start(); Console.WriteLine(); #if (!NETCOREAPP2_0 && !NETCOREAPP2_1 && !NETSTANDARD2_0) ConsoleExtensions.WriteLine(ConsoleColor.Green, $"[started] : '{_configuration.ServiceName}'."); #else ConsoleExtensions.WriteLine(ConsoleColor.Green, $"[started] : '{Assembly.GetEntryAssembly().FullName}'."); #endif Console.WriteLine(); ConsoleExtensions.WriteLine(ConsoleColor.DarkYellow, "[press ctrl+c to stop]"); Console.WriteLine(); WaitHandle.WaitAny(waitHandles); var stoppable = _service as IServiceHostStop; stoppable?.Stop(); var disposable = _service as IDisposable; disposable?.Dispose(); }
public bool Execute(ServiceConfiguration configuration) { Guard.AgainstNull(configuration, nameof(configuration)); var result = false; try { var arguments = new Arguments(Environment.GetCommandLineArgs()); var action = arguments.CommandLine.Length > 1 ? arguments.CommandLine[1] : string.Empty; configuration.WithArguments(arguments); if (ShouldShowHelp(arguments)) { ShowHelp(); return(true); } if (arguments.Contains("debug")) { Debugger.Launch(); } var install = arguments.Contains("install") || action.Equals("install", StringComparison.InvariantCultureIgnoreCase); var uninstall = arguments.Contains("uninstall") || action.Equals("uninstall", StringComparison.InvariantCultureIgnoreCase); if (install && uninstall) { throw new InstallException("Cannot specify /install and /uninstall together."); } var start = arguments.Contains("start") || action.Equals("start", StringComparison.InvariantCultureIgnoreCase); var stop = arguments.Contains("stop") || action.Equals("stop", StringComparison.InvariantCultureIgnoreCase); var timeoutValue = arguments.Get("timeout", "30000"); if (!int.TryParse(timeoutValue, out var timeout)) { timeout = 30000; } configuration.WithTimeout(timeout); if (uninstall) { new WindowsServiceInstaller().Uninstall(configuration); result = true; } if (install) { new WindowsServiceInstaller().Install(configuration); result = true; } if (stop || start) { var controller = new ServiceHostController(configuration); if (stop) { controller.Stop(); } if (start) { controller.Start(); } result = true; } } catch (Exception ex) { if (Environment.UserInteractive) { ConsoleExtensions.WriteLine(ConsoleColor.Red, ex.AllMessages()); Console.WriteLine(); ConsoleExtensions.WriteLine(ConsoleColor.Gray, "Press any key to close..."); Console.ReadKey(); } else { Log.Fatal($"[UNHANDLED EXCEPTION] : exception = {ex.AllMessages()}"); throw; } result = true; } return(result); }