public static void Execute( IScriptExecutionEnvironment environment, string serviceName, string configurationSetting) { CheckIfServiceExistsTask task = new CheckIfServiceExistsTask(serviceName, configurationSetting); task.Execute(environment); }
public static void Execute( ITaskContext environment, string machineName, string serviceName, string configurationSetting) { CheckIfServiceExistsTask task = new CheckIfServiceExistsTask(machineName, serviceName, configurationSetting); task.Execute(environment); }
/// <summary> /// Method defining the actual work for a task. /// </summary> /// <param name="environment">The script execution environment.</param> protected override void DoExecute(IScriptExecutionEnvironment environment) { string configSettingName = String.Format( System.Globalization.CultureInfo.InvariantCulture, "ServicesExist/{0}", serviceName); CheckIfServiceExistsTask.Execute(environment, serviceName, configSettingName); if (bool.Parse(environment.GetConfigSetting(configSettingName)) == true) { ControlWindowsServiceTask.Execute( environment, serviceName, ControlWindowsServiceMode.Stop, TimeSpan.FromSeconds(30)); } }
/// <summary> /// Method defining the actual work for a task. /// </summary> /// <param name="context">The script execution environment.</param> protected override void DoExecute(ITaskContext context) { string configSettingName = String.Format( System.Globalization.CultureInfo.InvariantCulture, "ServicesExist/{0}", serviceName); CheckIfServiceExistsTask.Execute(context, serviceName, configSettingName); if (context.Properties.Get <bool>(configSettingName)) { ControlWindowsServiceTask.Execute( context, serviceName, ControlWindowsServiceMode.Stop, TimeSpan.FromSeconds(30)); } }
/// <summary> /// Internal task execution code. /// </summary> /// <param name="context">The script execution environment.</param> protected override void DoExecute(ITaskContext context) { string configSettingName = String.Format( System.Globalization.CultureInfo.InvariantCulture, "ServicesExist/{0}", serviceName); CheckIfServiceExistsTask checkIfServiceExistsTask = new CheckIfServiceExistsTask(serviceName, configSettingName); checkIfServiceExistsTask.Execute(context); if (context.Properties.Get <bool>(configSettingName)) { switch (mode) { case InstallWindowsServiceMode.DoNothingIfExists: return; case InstallWindowsServiceMode.FailIfAlreadyInstalled: throw new TaskExecutionException( String.Format( System.Globalization.CultureInfo.InvariantCulture, "The Windows service '{0}' already exists.", serviceName)); case InstallWindowsServiceMode.ReinstallIfExists: UninstallWindowsServiceTask uninstallWindowsServiceTask = new UninstallWindowsServiceTask(executablePath); uninstallWindowsServiceTask.Execute(context); // wait for a while to ensure the service is really deleted SleepTask.Execute(context, serviceUninstallationWaitTime); break; default: throw new NotSupportedException(); } } IDictionary savedState = new Hashtable(); string[] commandLine = new string[0]; using (System.Configuration.Install.AssemblyInstaller assemblyInstaller = new System.Configuration.Install.AssemblyInstaller(executablePath, commandLine)) { try { assemblyInstaller.UseNewContext = true; assemblyInstaller.Install(savedState); assemblyInstaller.Commit(savedState); } catch (System.ComponentModel.Win32Exception ex) { // 1073 context.WriteInfo( "ex.ErrorCode = {0}", ex.NativeErrorCode); throw; } } }
protected override void DoExecute(ITaskContext context) { string configSettingName = String.Format( System.Globalization.CultureInfo.InvariantCulture, "ServicesExist/{0}", serviceName); CheckIfServiceExistsTask.Execute(context, MachineName, serviceName, configSettingName); if (!context.Properties.Get <bool>(configSettingName)) { if (FailIfNotExist) { throw new TaskExecutionException("Service {0} does not exist.", serviceName); } context.WriteInfo("Service '{0}' does not exist, doing nothing.", serviceName); return; } using (ServiceController serviceController = new ServiceController(serviceName, MachineName)) { ServiceControllerStatus status = ServiceControllerStatus.Running; switch (mode) { case ControlWindowsServiceMode.Start: status = ServiceControllerStatus.Running; break; case ControlWindowsServiceMode.Stop: status = ServiceControllerStatus.Stopped; break; } switch (status) { case ServiceControllerStatus.Running: if (serviceController.Status != ServiceControllerStatus.Running) { serviceController.Start(); } break; case ServiceControllerStatus.Stopped: if (serviceController.Status != ServiceControllerStatus.Stopped) { serviceController.Stop(); } break; } int timeSoFar = 0; for (serviceController.Refresh(); serviceController.Status != status; serviceController.Refresh()) { System.Threading.Thread.Sleep(500); timeSoFar += 500; if (timeSoFar >= timeout.TotalMilliseconds) { throw new TaskExecutionException( String.Format( System.Globalization.CultureInfo.InvariantCulture, "Timeout waiting for '{0}' service to reach status {1}.", serviceName, status)); } } } }