private void DoCreateService(ServiceControlManager serviceControlManager, ServiceDefinition serviceDefinition, bool startImmediately) { using (ServiceHandle svc = serviceControlManager.CreateService(serviceDefinition.ServiceName, serviceDefinition.DisplayName, serviceDefinition.BinaryPath, ServiceType.Win32OwnProcess, serviceDefinition.AutoStart ? ServiceStartType.AutoStart : ServiceStartType.StartOnDemand, serviceDefinition.ErrorSeverity, serviceDefinition.Credentials)) { string description = serviceDefinition.Description; if (!string.IsNullOrEmpty(description)) { svc.SetDescription(description); } ServiceFailureActions serviceFailureActions = serviceDefinition.FailureActions; if (serviceFailureActions != null) { svc.SetFailureActions(serviceFailureActions); svc.SetFailureActionFlag(serviceDefinition.FailureActionsOnNonCrashFailures); } if (serviceDefinition.AutoStart && serviceDefinition.DelayedAutoStart) { svc.SetDelayedAutoStartFlag(true); } if (startImmediately) { svc.Start(); } } }
private void ThenTheServiceHasBeenUpdated(ServiceHandle serviceHandle, ServiceStartType serviceStartType) { A.CallTo( () => serviceHandle.ChangeConfig(TestServiceDisplayName, TestServiceBinaryPath, ServiceType.Win32OwnProcess, serviceStartType, TestServiceErrorSeverity, TestCredentials)) .MustHaveHappened(); A.CallTo( () => nativeInterop.ChangeServiceConfigW(serviceHandle, ServiceType.Win32OwnProcess, serviceStartType, TestServiceErrorSeverity, TestServiceBinaryPath, null, IntPtr.Zero, null, TestCredentials.UserName, TestCredentials.Password, TestServiceDisplayName)) .MustHaveHappened(); A.CallTo(() => serviceHandle.SetDescription(TestServiceDescription)) .MustHaveHappened(); // interop logic for SetDescription() is covered in ServiceCreationTests }
/// <summary> /// Installs the service. /// </summary> /// <param name="DisplayName">Service display name.</param> /// <param name="Description">Service description.</param> /// <param name="StartType">How the service should be started.</param> /// <param name="StartImmediately">If the service should be started immediately.</param> /// <param name="FailureActions">Service failure actions.</param> /// <param name="Credentials">Credentials to use when running service.</param> /// <returns> /// Return code: /// /// 0: Installed, not started. /// 1: Installed, started. /// 2: Updated, not started. /// 3: Updated, started. /// </returns> /// <exception cref="Exception">If service could not be installed.</exception> public int Install(string DisplayName, string Description, ServiceStartType StartType, bool StartImmediately, ServiceFailureActions FailureActions, Win32ServiceCredentials Credentials) { string Path = Assembly.GetExecutingAssembly().Location.Replace(".dll", ".exe"); try { using (ServiceControlManager mgr = ServiceControlManager.Connect(null, null, ServiceControlManagerAccessRights.All)) { if (mgr.TryOpenService(this.serviceName, ServiceControlAccessRights.All, out ServiceHandle existingService, out Win32Exception errorException)) { using (existingService) { existingService.ChangeConfig(DisplayName, Path, ServiceType.Win32OwnProcess, StartType, ErrorSeverity.Normal, Credentials); if (!string.IsNullOrEmpty(Description)) { existingService.SetDescription(Description); } if (FailureActions != null) { existingService.SetFailureActions(FailureActions); existingService.SetFailureActionFlag(true); } else { existingService.SetFailureActionFlag(false); } if (StartImmediately) { existingService.Start(throwIfAlreadyRunning: false); return(3); } else { return(2); } } } else { if (errorException.NativeErrorCode == Win32.ERROR_SERVICE_DOES_NOT_EXIST) { using (ServiceHandle svc = mgr.CreateService(this.serviceName, DisplayName, Path, ServiceType.Win32OwnProcess, StartType, ErrorSeverity.Normal, Credentials)) { if (!string.IsNullOrEmpty(Description)) { svc.SetDescription(Description); } if (FailureActions != null) { svc.SetFailureActions(FailureActions); svc.SetFailureActionFlag(true); } else { svc.SetFailureActionFlag(false); } if (StartImmediately) { svc.Start(); return(1); } else { return(0); } } } else { throw errorException; } } }