public void Install(WindowsServiceConfiguration windowsService) { Func <ServiceController, bool> func2 = null; if (windowsService == null) { throw new ArgumentNullException("windowsService"); } if (!string.IsNullOrWhiteSpace(this._machineName)) { throw new InvalidOperationException("Not supported on remote machines."); } using (ServiceProcessInstaller serviceProcessInstaller = new ServiceProcessInstaller()) { using (ServiceInstaller serviceInstaller = windowsService.CreateInstaller(serviceProcessInstaller)) { if (windowsService.Args != null) { serviceInstaller.AfterInstall += new InstallEventHandler((object sender, InstallEventArgs installArgs) => { ServiceController[] services = this.GetServices(); Func <ServiceController, bool> u003cu003e9_1 = func2; if (u003cu003e9_1 == null) { Func <ServiceController, bool> func = (ServiceController x) => x.ServiceName.Equals(serviceInstaller.ServiceName); Func <ServiceController, bool> func1 = func; func2 = func; u003cu003e9_1 = func1; } ServiceController serviceController = ((IEnumerable <ServiceController>)services).SingleOrDefault <ServiceController>(u003cu003e9_1); if (serviceController != null) { WindowsServices.Win32Service.SetServiceArguments(serviceController, windowsService.ExePath, windowsService.Args); } WindowsServices.Dispose(services); }); } serviceInstaller.Install(new Hashtable()); } } }
private IDisposable GetServices(Action <ServiceController[]> services) { ServiceController[] serviceControllerArray = this.GetServices(); services(serviceControllerArray); return(new DisposableAction(() => WindowsServices.Dispose(serviceControllerArray))); }