public static void InstallService(string serviceName, string customArg) { try { var savedState = new Hashtable(); var installContext = new InstallContext(Path.ChangeExtension(ExecutablePath, ".InstallLog"), new[] { string.Empty }); installContext.Parameters["assemblypath"] = string.IsNullOrEmpty(customArg) ? ExecutablePath : ExecutablePath + " \"" + customArg + "\""; var hostServiceInstaller = new HostServiceInstaller(serviceName) { Context = installContext }; hostServiceInstaller.Install(savedState); hostServiceInstaller.Commit(savedState); } catch (Exception ex) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Service install failed:"); Console.WriteLine(ex); Console.ResetColor(); } }
public void UninstallService(HostSettings settings, Action beforeUninstall, Action afterUninstall) { if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { throw new PlatformNotSupportedException("Not running windows"); } using (var installer = new HostServiceInstaller(settings)) { Action <InstallEventArgs> before = x => { if (beforeUninstall != null) { beforeUninstall(); } }; Action <InstallEventArgs> after = x => { if (afterUninstall != null) { afterUninstall(); } }; installer.UninstallService(before, after); } }
public void Execute() { _log.Info("Received service install notification"); if (WinServiceHelper.IsInstalled(_settings.ServiceName.FullName)) { string message = string.Format("The {0} service has already been installed.", _settings.ServiceName.FullName); _log.Error(message); return; } var installer = new HostServiceInstaller(_settings); WinServiceHelper.Register(_settings.ServiceName.FullName, installer); }
public static void UninstallService(string serviceName) { try { var installContext = new InstallContext(Path.ChangeExtension(ExecutablePath, ".InstallLog"), new[] { string.Empty }); installContext.Parameters["assemblypath"] = ExecutablePath; var hostServiceInstaller = new HostServiceInstaller(serviceName) { Context = installContext }; hostServiceInstaller.Uninstall(null); } catch (Exception ex) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Service uninstall failed:"); Console.WriteLine(ex); Console.ResetColor(); } }
public void InstallService(InstallHostSettings settings, Action <InstallHostSettings> beforeInstall, Action afterInstall, Action beforeRollback, Action afterRollback) { if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { throw new PlatformNotSupportedException("Not running windows"); } using (var installer = new HostServiceInstaller(settings)) { Action <InstallEventArgs> before = x => { if (beforeInstall != null) { beforeInstall(settings); installer.ServiceProcessInstaller.Username = settings.Credentials.Username; installer.ServiceProcessInstaller.Account = settings.Credentials.Account; bool gMSA = false; // Group Managed Service Account (gMSA) workaround per // https://connect.microsoft.com/VisualStudio/feedback/details/795196/service-process-installer-should-support-virtual-service-accounts if (settings.Credentials.Account == ServiceAccount.User && settings.Credentials.Username != null && ((gMSA = settings.Credentials.Username.EndsWith("$", StringComparison.InvariantCulture)) || string.Equals(settings.Credentials.Username, "NT SERVICE\\" + settings.ServiceName, StringComparison.InvariantCulture))) { _log.InfoFormat(gMSA ? "Installing as gMSA {0}." : "Installing as virtual service account", settings.Credentials.Username); installer.ServiceProcessInstaller.Password = null; installer.ServiceProcessInstaller .GetType() .GetField("haveLoginInfo", BindingFlags.Instance | BindingFlags.NonPublic) .SetValue(installer.ServiceProcessInstaller, true); } else { installer.ServiceProcessInstaller.Password = settings.Credentials.Password; } } }; Action <InstallEventArgs> after = x => { if (afterInstall != null) { afterInstall(); } }; Action <InstallEventArgs> before2 = x => { if (beforeRollback != null) { beforeRollback(); } }; Action <InstallEventArgs> after2 = x => { if (afterRollback != null) { afterRollback(); } }; installer.InstallService(before, after, before2, after2); } }
public void Execute() { if (!WinServiceHelper.IsInstalled(_settings.ServiceName.FullName)) { string message = string.Format("The {0} service has not been installed.", _settings.ServiceName.FullName); _log.Error(message); return; } if (!CommandUtil.IsAdministrator) { if (Environment.OSVersion.Version.Major == 6) { var startInfo = new ProcessStartInfo(Assembly.GetEntryAssembly().Location, _commandLine); startInfo.Verb = "runas"; startInfo.UseShellExecute = true; startInfo.CreateNoWindow = true; try { Process process = Process.Start(startInfo); process.WaitForExit(); return; } catch (Win32Exception ex) { _log.Debug("Process Start Exception", ex); } } _log.ErrorFormat("The {0} service can only be uninstalled as an administrator", _settings.ServiceName.FullName); return; } var installer = new HostServiceInstaller(_settings); WinServiceHelper.Unregister(_settings.ServiceName.FullName, installer, _settings.AfterUninstallAction); }