コード例 #1
0
		public void InstallService(InstallHostSettings settings, IDictionary<string, object> arguments = null, Action<InstallEventArgs> beforeInstall = null, Action<InstallEventArgs> afterInstall = null, Action<InstallEventArgs> beforeRollback = null, Action<InstallEventArgs> afterRollback = null)
		{
			var commandLine = BuildCommandLine(settings, arguments);

			using (var installer = CreateInstaller(settings, commandLine))
			{
				if (beforeInstall != null)
				{
					installer.BeforeInstall += (sender, args) => beforeInstall(args);
				}

				if (afterInstall != null)
				{
					installer.AfterInstall += (sender, args) => afterInstall(args);
				}

				if (beforeRollback != null)
				{
					installer.BeforeRollback += (sender, args) => beforeRollback(args);
				}

				if (afterRollback != null)
				{
					installer.AfterRollback += (sender, args) => afterRollback(args);
				}

				Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);

				installer.Install(new Hashtable());
			}
		}
コード例 #2
0
ファイル: InstallHost.cs プロジェクト: zyj0021/Topshelf
 void ExecutePreActions(InstallHostSettings settings)
 {
     foreach (Action <InstallHostSettings> action in _preActions)
     {
         action(_installSettings);
     }
 }
コード例 #3
0
        public void InstallService(InstallHostSettings settings, IDictionary <string, object> arguments = null, Action <InstallEventArgs> beforeInstall = null, Action <InstallEventArgs> afterInstall = null, Action <InstallEventArgs> beforeRollback = null, Action <InstallEventArgs> afterRollback = null)
        {
            var commandLine = BuildCommandLine(settings, arguments);

            using (var installer = CreateInstaller(settings, commandLine))
            {
                if (beforeInstall != null)
                {
                    installer.BeforeInstall += (sender, args) => beforeInstall(args);
                }

                if (afterInstall != null)
                {
                    installer.AfterInstall += (sender, args) => afterInstall(args);
                }

                if (beforeRollback != null)
                {
                    installer.BeforeRollback += (sender, args) => beforeRollback(args);
                }

                if (afterRollback != null)
                {
                    installer.AfterRollback += (sender, args) => afterRollback(args);
                }

                Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);

                installer.Install(new Hashtable());
            }
        }
コード例 #4
0
        public void InstallService(
            InstallHostSettings settings,
            Action <InstallHostSettings> beforeInstall,
            Action afterInstall,
            Action beforeRollback,
            Action afterRollback)
        {
            using (var installer = new SpHostServiceInstaller(settings, _hostConfigurator))
            {
                Action <InstallEventArgs> before = x =>
                {
                    beforeInstall?.Invoke(settings);
                };

                Action <InstallEventArgs> after = x =>
                {
                    afterInstall?.Invoke();
                };

                Action <InstallEventArgs> before2 = x =>
                {
                    beforeRollback?.Invoke();
                };

                Action <InstallEventArgs> after2 = x =>
                {
                    afterRollback?.Invoke();
                };

                installer.InstallService(before, after, before2, after2);
            }
        }
コード例 #5
0
        public void InstallService(
            InstallHostSettings settings, 
            Action<InstallHostSettings> beforeInstall, 
            Action afterInstall, 
            Action beforeRollback,
            Action afterRollback)
        {
            using (var installer = new SpHostServiceInstaller(settings, _hostConfigurator))
            {
                Action<InstallEventArgs> before = x =>
                {
                    beforeInstall?.Invoke(settings);
                };

                Action<InstallEventArgs> after = x =>
                {
                    afterInstall?.Invoke();
                };

                Action<InstallEventArgs> before2 = x =>
                {
                    beforeRollback?.Invoke();
                };

                Action<InstallEventArgs> after2 = x =>
                {
                    afterRollback?.Invoke();
                };

                installer.InstallService(before, after, before2, after2);
            }
        }
コード例 #6
0
        public void InstallService(InstallHostSettings settings, Action <InstallHostSettings> beforeInstall, Action afterInstall, Action beforeRollback, Action afterRollback)
        {
            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;

                        // 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 &&
                            settings.Credentials.Username.EndsWith("$", StringComparison.InvariantCulture))
                        {
                            _log.InfoFormat("Installing as gMSA {0}.", 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);
            }
        }
コード例 #7
0
        public SpHostServiceInstaller(InstallHostSettings settings, HostConfigurator configurator)
        {
            _hostConfigurator = configurator;

            _installer = CreateInstaller(settings);

            _transactedInstaller = CreateTransactedInstaller(_installer);
        }
コード例 #8
0
        public SpHostServiceInstaller(InstallHostSettings settings, HostConfigurator configurator)
        {
            _hostConfigurator = configurator;

            _installer = CreateInstaller(settings);

            _transactedInstaller = CreateTransactedInstaller(_installer);
        }
コード例 #9
0
ファイル: Program.cs プロジェクト: uk-gov-mirror/jncc.nbnv
        private static void EnsureConfiguration(InstallHostSettings installHostSettings)
        {
            var fsm = new FileSystemManger();

            fsm.EnsureFolder(_config.SourceFolder);
            fsm.EnsureFolder(_config.ImporterLogFolder);
            fsm.EnsureFolder(_config.ResultFolder);
            fsm.EnsureFolder(_config.TempFolder);
        }
コード例 #10
0
        static Installer CreateInstaller(InstallHostSettings settings)
        {
            var installers = new Installer[]
            {
                ConfigureServiceInstaller(settings, settings.Dependencies, settings.StartMode),
                ConfigureServiceProcessInstaller(settings.Account, settings.Username, settings.Password)
            };

            return(CreateHostInstaller(settings, installers));
        }
コード例 #11
0
ファイル: HostServiceInstaller.cs プロジェクト: qed-/Topshelf
        static Installer CreateInstaller(InstallHostSettings settings)
        {
            var installers = new Installer[]
                {
                    ConfigureServiceInstaller(settings, settings.Dependencies, settings.StartMode),
                    ConfigureServiceProcessInstaller(settings.Account, settings.Username, settings.Password)
                };

            return CreateHostInstaller(settings, installers);
        }
コード例 #12
0
        public void InstallService(InstallHostSettings settings, Action<InstallHostSettings> beforeInstall, Action afterInstall, Action beforeRollback, Action afterRollback)
        {
            var installer = new LinuxHostServiceInstallerImpl();

            Action<InstallEventArgs> tryBeforeInstall = x => beforeInstall?.Invoke(settings);
            Action<InstallEventArgs> tryAfterInstall = x => afterInstall?.Invoke();
            Action<InstallEventArgs> tryBeforeRollback = x => beforeRollback?.Invoke();
            Action<InstallEventArgs> tryAfterRollback = x => afterRollback?.Invoke();

            installer.InstallService(settings, _arguments, tryBeforeInstall, tryAfterInstall, tryBeforeRollback, tryAfterRollback);
        }
コード例 #13
0
        public void InstallService(InstallHostSettings settings, Action <InstallHostSettings> beforeInstall, Action afterInstall, Action beforeRollback, Action afterRollback)
        {
            var installer = new LinuxHostServiceInstallerImpl();

            Action <InstallEventArgs> tryBeforeInstall  = x => beforeInstall?.Invoke(settings);
            Action <InstallEventArgs> tryAfterInstall   = x => afterInstall?.Invoke();
            Action <InstallEventArgs> tryBeforeRollback = x => beforeRollback?.Invoke();
            Action <InstallEventArgs> tryAfterRollback  = x => afterRollback?.Invoke();

            installer.InstallService(settings, _arguments, tryBeforeInstall, tryAfterInstall, tryBeforeRollback, tryAfterRollback);
        }
コード例 #14
0
        public InstallHost(HostEnvironment environment, HostSettings settings, HostStartMode startMode,
            IEnumerable<string> dependencies,
            Credentials credentials, IEnumerable<Action> preActions, IEnumerable<Action> postActions, bool sudo)
        {
            _environment = environment;
            _settings = settings;

            _installSettings = new InstallServiceSettingsImpl(settings, credentials, startMode, dependencies.ToArray());

            _preActions = preActions;
            _postActions = postActions;
            _sudo = sudo;
        }
コード例 #15
0
ファイル: InstallHost.cs プロジェクト: youngcm2/Topshelf
        public InstallHost(HostEnvironment environment, HostSettings settings, HostStartMode startMode,
                           IEnumerable <string> dependencies,
                           Credentials credentials, IEnumerable <Action> preActions, IEnumerable <Action> postActions, bool sudo)
        {
            _environment = environment;
            _settings    = settings;

            _installSettings = new InstallServiceSettingsImpl(settings, credentials, startMode, dependencies.ToArray());

            _preActions  = preActions;
            _postActions = postActions;
            _sudo        = sudo;
        }
コード例 #16
0
        private Installer CreateInstaller(InstallHostSettings settings)
        {
            var installers = new Installer[]
            {
                ConfigureServiceInstaller(settings, settings.Dependencies, settings.StartMode),
                ConfigureServiceProcessInstaller(settings.Credentials.Account, settings.Credentials.Username, settings.Credentials.Password)
            };

            //DO not auto create EventLog Source while install service
            //MSDN: When the installation is performed, it automatically creates an EventLogInstaller to install the event log source associated with the ServiceBase derived class. The Log property for this source is set by the ServiceInstaller constructor to the computer's Application log. When you set the ServiceName of the ServiceInstaller (which should be identical to the ServiceBase..::.ServiceName of the service), the Source is automatically set to the same value. In an installation failure, the source's installation is rolled-back along with previously installed services.
            //MSDN: from EventLog.CreateEventSource Method (String, String) : an ArgumentException thrown when The first 8 characters of logName match the first 8 characters of an existing event log name.
            RemoveEventLogInstallers(installers);

            return(CreateHostInstaller(settings, installers));
        }
コード例 #17
0
        public void InstallService(InstallHostSettings settings, Action beforeInstall, Action afterInstall)
        {
            using (var installer = new HostServiceInstaller(settings))
            {
                Action<InstallEventArgs> before = x =>
                    {
                        if (beforeInstall != null)
                            beforeInstall();
                    };

                Action<InstallEventArgs> after = x =>
                    {
                        if (afterInstall != null)
                            afterInstall();
                    };

                installer.InstallService(before, after);
            }
        }
コード例 #18
0
        private static void AddServiceStartupOption(InstallHostSettings settings, IEnumerable <string> paths)
        {
            using (RegistryKey system = Registry.LocalMachine.OpenSubKey("System"))
            {
                if (system == null)
                {
                    Logger.Error(@"Не удалось найти в системном реестре ключ HKEY_LOCAL_MACHINE\SYSTEM");
                    return;
                }

                using (RegistryKey currentControlSet = system.OpenSubKey("CurrentControlSet"))
                {
                    if (currentControlSet == null)
                    {
                        Logger.Error(@"Не удалось найти в системно реестре ключ HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet");
                        return;
                    }

                    using (RegistryKey services = currentControlSet.OpenSubKey("Services"))
                    {
                        if (services == null)
                        {
                            Logger.Error(@"Не удалось найти в системно реестре ключ HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services");
                            return;
                        }

                        using (RegistryKey service = services.OpenSubKey(settings.ServiceName, true))
                        {
                            if (service == null)
                            {
                                Logger.Error(@"Не удалось найти в системно реестре ключ HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\" + settings.ServiceName);
                                return;
                            }

                            var imagePath = (string)service.GetValue("ImagePath");
                            imagePath += " -path:" + string.Join(PathSeparator, paths);
                            service.SetValue("ImagePath", imagePath);
                        }
                    }
                }
            }
        }
コード例 #19
0
        public void InstallService(InstallHostSettings settings, Action <InstallHostSettings> beforeInstall, Action afterInstall, Action beforeRollback, Action afterRollback)
        {
            using (var installer = new HostServiceInstaller(settings))
            {
                Action <InstallEventArgs> before = x =>
                {
                    if (beforeInstall != null)
                    {
                        beforeInstall(settings);
                        installer.ServiceProcessInstaller.Username = settings.Credentials.Username;
                        installer.ServiceProcessInstaller.Password = settings.Credentials.Password;
                        installer.ServiceProcessInstaller.Account  = settings.Credentials.Account;
                    }
                };

                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);
            }
        }
コード例 #20
0
        protected override Installer CreateInstaller(InstallHostSettings settings, string commandLine)
        {
            var baseInstallers = new Installer[]
            {
                CreateServiceInstaller(settings, settings.Dependencies, settings.StartMode),
                CreateServiceProcessInstaller(settings.Credentials?.Account, settings.Credentials?.Username, settings.Credentials?.Password)
            };

            foreach (var installer in baseInstallers)
            {
                var eventLogInstallers = installer.Installers.OfType <EventLogInstaller>().ToArray();

                foreach (var eventLogInstaller in eventLogInstallers)
                {
                    installer.Installers.Remove(eventLogInstaller);
                }
            }

            var mainInstaller = new HostInstaller(settings, commandLine, baseInstallers);

            return(CreateTransactedInstaller(mainInstaller));
        }
コード例 #21
0
        static Installer CreateInstaller(InstallHostSettings settings)
        {
            var installers = new Installer[]
                {
                    ConfigureServiceInstaller(settings, settings.Dependencies, settings.StartMode),
                    ConfigureServiceProcessInstaller(settings.Account, settings.Username, settings.Password)
                };

            //DO not auto create EventLog Source while install service
            //MSDN: When the installation is performed, it automatically creates an EventLogInstaller to install the event log source associated with the ServiceBase derived class. The Log property for this source is set by the ServiceInstaller constructor to the computer's Application log. When you set the ServiceName of the ServiceInstaller (which should be identical to the ServiceBase..::.ServiceName of the service), the Source is automatically set to the same value. In an installation failure, the source's installation is rolled-back along with previously installed services.
            //MSDN: from EventLog.CreateEventSource Method (String, String) : an ArgumentException thrown when The first 8 characters of logName match the first 8 characters of an existing event log name.
            foreach (var installer in installers)
            {
                var eventLogInstallers = installer.Installers.OfType<EventLogInstaller>().ToArray();
                foreach (var eventLogInstaller in eventLogInstallers)
                {
                    installer.Installers.Remove(eventLogInstaller);
                }
            }

            return CreateHostInstaller(settings, installers);
        }
コード例 #22
0
        public void InstallService(InstallHostSettings settings, Action beforeInstall, Action afterInstall, Action beforeRollback, Action afterRollback)
        {
            using (var installer = new HostServiceInstaller(settings))
            {
                Action <InstallEventArgs> before = x =>
                {
                    if (beforeInstall != null)
                    {
                        beforeInstall();
                    }
                };

                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);
            }
        }
コード例 #23
0
		protected override Installer CreateInstaller(InstallHostSettings settings, string commandLine)
		{
			var baseInstallers = new Installer[]
			{
				CreateServiceInstaller(settings, settings.Dependencies, settings.StartMode),
				CreateServiceProcessInstaller(settings.Account, settings.Username, settings.Password)
			};

			foreach (var installer in baseInstallers)
			{
				var eventLogInstallers = installer.Installers.OfType<EventLogInstaller>().ToArray();

				foreach (var eventLogInstaller in eventLogInstallers)
				{
					installer.Installers.Remove(eventLogInstaller);
				}
			}

			var mainInstaller = new HostInstaller(settings, commandLine, baseInstallers);

			return CreateTransactedInstaller(mainInstaller);
		}
コード例 #24
0
		public void InstallService(InstallHostSettings settings, Action beforeInstall, Action afterInstall, Action beforeRollback, Action afterRollback)
		{
			var installer = new LinuxHostServiceInstallerImpl();

			Action<InstallEventArgs> tryBeforeInstall = x =>
				{
					if (beforeInstall != null)
					{
						beforeInstall();
					}
				};

			Action<InstallEventArgs> tryAfterInstall = x =>
			{
				if (afterInstall != null)
				{
					afterInstall();
				}
			};

			Action<InstallEventArgs> tryBeforeRollback = x =>
			{
				if (beforeRollback != null)
				{
					beforeRollback();
				}
			};

			Action<InstallEventArgs> tryAfterRollback = x =>
			{
				if (afterRollback != null)
				{
					afterRollback();
				}
			};

			installer.InstallService(settings, _arguments, tryBeforeInstall, tryAfterInstall, tryBeforeRollback, tryAfterRollback);
		}
コード例 #25
0
 public void InstallService(InstallHostSettings settings, Action <InstallHostSettings> beforeInstall, Action afterInstall, Action beforeRollback, Action afterRollback)
 {
     throw new NotImplementedException();
 }
コード例 #26
0
ファイル: InstallHost.cs プロジェクト: Topshelf/Topshelf
 void ExecutePreActions(InstallHostSettings settings)
 {
     foreach (Action<InstallHostSettings> action in _preActions)
     {
         action(_installSettings);
     }
 }
コード例 #27
0
 protected override Installer CreateInstaller(InstallHostSettings settings, string commandLine)
 {
     return(new LsbLinuxHostInstaller(CreateServiceSettings(settings, commandLine), null, CreateServiceLogWriter()));
 }
コード例 #28
0
		protected abstract Installer CreateInstaller(InstallHostSettings settings, string commandLine);
コード例 #29
0
		public void InstallService(InstallHostSettings settings, Action beforeInstall, Action afterInstall, Action beforeRollback, Action afterRollback)
		{
			throw new NotImplementedException();
		}
コード例 #30
0
        void ConfigureServiceRecovery(InstallHostSettings installSettings)
        {
            var controller = new WindowsServiceRecoveryController();

            controller.SetServiceRecoveryOptions(_settings, _options);
        }
コード例 #31
0
        public void InstallService(InstallHostSettings settings, Action<InstallHostSettings> beforeInstall, Action afterInstall, Action beforeRollback, Action afterRollback)
        {
            using (var installer = new HostServiceInstaller(settings))
            {
                Action<InstallEventArgs> before = x =>
                    {
                        if (beforeInstall != null)
                        {
                            beforeInstall(settings);
                            installer.ServiceProcessInstaller.Username = settings.Credentials.Username;
                            installer.ServiceProcessInstaller.Password = settings.Credentials.Password;
                            installer.ServiceProcessInstaller.Account = settings.Credentials.Account;
                        }
                    };

                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);
            }
        }
コード例 #32
0
        public HostServiceInstaller(InstallHostSettings settings)
        {
            _installer = CreateInstaller(settings);

            _transactedInstaller = CreateTransactedInstaller(_installer);
        }
コード例 #33
0
        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);
            }
        }
コード例 #34
0
ファイル: HostServiceInstaller.cs プロジェクト: qed-/Topshelf
        public HostServiceInstaller(InstallHostSettings settings)
        {
            _installer = CreateInstaller(settings);

            _transactedInstaller = CreateTransactedInstaller(_installer);
        }
コード例 #35
0
 void ConfigureServiceRecovery(InstallHostSettings installSettings)
 {
     var controller = new WindowsServiceRecoveryController();
     controller.SetServiceRecoveryOptions(_settings, _options);
 }
コード例 #36
0
 public void InstallService(InstallHostSettings settings, Action <InstallHostSettings> beforeInstall, Action afterInstall, Action beforeRollback, Action afterRollback)
 {
     wrappedHost.InstallService(settings, beforeInstall, afterInstall, beforeRollback, afterRollback);
 }
コード例 #37
0
 protected abstract Installer CreateInstaller(InstallHostSettings settings, string commandLine);
コード例 #38
0
 protected override Installer CreateInstaller(InstallHostSettings settings, string commandLine)
 {
     return new LsbLinuxHostInstaller(CreateServiceSettings(settings, commandLine), null, CreateServiceLogWriter());
 }
コード例 #39
0
        public void InstallService(InstallHostSettings settings, Action<InstallHostSettings> beforeInstall, Action afterInstall, Action beforeRollback, Action afterRollback)
        {
            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;

                            // 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 &&
                                settings.Credentials.Username.EndsWith("$", StringComparison.InvariantCulture))
                            {
                                _log.InfoFormat("Installing as gMSA {0}.", 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);
            }
        }