public ServiceWrapper() { var conf = LocalServiceInstallerConfiguration.CreateFromConfiguration(); this.ServiceName = conf.ServiceName; this.CanStop = true; this.CanPauseAndContinue = false; this.AutoLog = true; }
public LocalServiceInstaller() { var config = ConfigurationManager.OpenExeConfiguration(typeof(LocalServiceInstaller).Assembly.CodeBase.Replace("file:///", string.Empty)); if (config == null) { throw new InvalidOperationException("Configuration file is missing. "); } var conf = LocalServiceInstallerConfiguration.CreateFromConfiguration(config); if (conf == null) { throw new InvalidOperationException("Configuration entries are missing. "); } // configure ServiceProcessInstaller this.processInstaller = new ServiceProcessInstaller() { Account = conf.ServiceAccount, }; if (conf.ServiceAccount == ServiceAccount.User) { this.processInstaller.Username = conf.Username; this.processInstaller.Password = conf.Password; } this.Installers.Add(this.processInstaller); // configure ServiceInstaller this.serviceInstaller = new ServiceInstaller() { DisplayName = conf.DisplayName, ServiceName = conf.ServiceName, StartType = conf.StartType, DelayedAutoStart = conf.DelayedAutoStart, ServicesDependedOn = conf.ServicesDependedOn, // don't let it be null! Description = conf.Description, }; this.Installers.Add(this.serviceInstaller); }
private static LocalServiceInstallerConfiguration CreateFromConfiguration(Func <string, string> appSettings) { var cfg = new LocalServiceInstallerConfiguration(); var serviceAccountValue = appSettings("ServiceAccount"); if (string.IsNullOrEmpty(serviceAccountValue)) { cfg.ServiceAccount = ServiceAccount.NetworkService; } else { cfg.ServiceAccount = (ServiceAccount)Enum.Parse(typeof(ServiceAccount), serviceAccountValue); if (cfg.ServiceAccount == ServiceAccount.User) { cfg.Username = appSettings("Username"); cfg.Password = appSettings("Password"); } } var startTypeValue = appSettings("StartType"); if (string.IsNullOrEmpty(startTypeValue)) { cfg.StartType = ServiceStartMode.Automatic; } else { cfg.StartType = (ServiceStartMode)Enum.Parse(typeof(ServiceStartMode), startTypeValue); } var delayedAutoStartValue = appSettings("DelayedAutoStart"); if (string.IsNullOrEmpty(delayedAutoStartValue)) { cfg.DelayedAutoStart = false; } else { cfg.DelayedAutoStart = bool.Parse(delayedAutoStartValue); } var dependenciesValue = appSettings("ServicesDependedOn"); if (!string.IsNullOrWhiteSpace(dependenciesValue)) { cfg.ServicesDependedOn = dependenciesValue .Split(new string[] { ",", ";", " ", }, StringSplitOptions.RemoveEmptyEntries) .Select(e => e.Trim()) .Where(e => !string.IsNullOrWhiteSpace(e)) .ToArray(); } else { cfg.ServicesDependedOn = new string[0]; } cfg.ServiceName = appSettings("ServiceName"); cfg.DisplayName = appSettings("DisplayName"); cfg.Description = appSettings("Description"); return(cfg); }