private void UpdateServiceSettings()

in Configurator/Core/Server/ServerConfigurationController.cs [3661:3752]


    private void UpdateServiceSettings()
    {
      CancellationToken.ThrowIfCancellationRequested();

      // If server was previously configured as a service but now it will run as a process.
      bool existingService = (OldSettings != null
                              && OldSettings.ServiceExists());
      bool isNew = ConfigurationType == ConfigurationType.Configure;
      if (existingService
          && !isNew
          && ConfigurationType != ConfigurationType.Upgrade
          && (!Settings.ConfigureAsService
              || OldSettings.ServiceName != Settings.ServiceName))
      {
        // Delete and report as the service not existing.
        ReportStatus(Resources.ServerConfigDeletingExistingService);
        var deleteOperationSucceeded = MySqlServiceControlManager.Delete(OldSettings.ServiceName);
        if (deleteOperationSucceeded != null)
        {
          RebootRequired = !Convert.ToBoolean(deleteOperationSucceeded);
          ReportStatus(RebootRequired
                      ? Resources.ServiceRemoveAfterRebootText
                      : Resources.ServiceRemovedText);
        }
        else
        {
          ReportStatus(Resources.ServiceManualRemoveRequiredText);
        }

        existingService = false;
      }

      CancellationToken.ThrowIfCancellationRequested();
      if (Settings.ConfigureAsService)
      {
        CheckServicePermissions();
        var cmd = $"\"{Path.Combine(InstallDirectory, BINARY_DIRECTORY_NAME, SERVER_EXECUTABLE_FILENAME)}\" --defaults-file=\"{Path.Combine(Settings.IniDirectory, Settings.ConfigFile)}\" {Settings.ServiceName}";
        if (existingService 
          && !isNew)
        {
          var newServiceName = ConfigurationType == ConfigurationType.Upgrade
            && IsServiceRenameNeeded
              ? Settings.GetDefaultServiceName()
              : Settings.ServiceName;
          ReportStatus(Resources.ServerConfigUpdatingExistingService);
          if (!newServiceName.Equals(Settings.ServiceName))
          {
            _revertController.OldServiceName = ExistingServerInstallationInstance.ServiceName;
            _revertController.NewServiceName = newServiceName;
            _revertController.ServiceCommand = Regex.Replace(cmd, newServiceName, ExistingServerInstallationInstance.ServiceName, RegexOptions.IgnoreCase);
            _revertController.ServiceCommand = _revertController.ServiceCommand.Replace(ServerVersion.ToString(2), ExistingServerInstallationInstance.ServerVersion.ToString(2));
            _revertController.ServiceRenamed = true;
            ReportStatus(string.Format(Resources.ServerConfigUpdatingExistingServiceWithNewName, ExistingServerInstallationInstance.ServiceName, newServiceName));
            cmd = $"\"{Path.Combine(InstallDirectory, BINARY_DIRECTORY_NAME, SERVER_EXECUTABLE_FILENAME)}\" --defaults-file=\"{Path.Combine(Settings.IniDirectory, Settings.ConfigFile)}\" {newServiceName}";
            MySqlServiceControlManager.Delete(ExistingServerInstallationInstance.ServiceName);
            MySqlServiceControlManager.Add(newServiceName, newServiceName, cmd, Settings.ServiceAccountUsername, Settings.ServiceAccountPassword, Settings.ServiceStartAtStartup);
            Settings.ServiceName = newServiceName;

            // If new service does not exist fail step.
            if (!Settings.ServiceExists())
            {
              RevertedSteps = _revertController.Rollback(OldSettings);
              CurrentStep.Status = ConfigurationStepStatus.Error;
            }

            return;
          }
          else
          {
            MySqlServiceControlManager.Update(ConfigurationType != ConfigurationType.Upgrade
            ? Settings.ServiceName
            : OldSettings.ServiceName,
            Settings.ServiceName,
            Settings.ServiceName,
            cmd,
            Settings.ServiceAccountUsername,
            Settings.ServiceAccountPassword,
            Settings.ServiceStartAtStartup);
          }
          
          ReportStatus(Resources.ServerConfigServiceUpdated);
        }
        else
        {
          ReportStatus(Resources.ServerConfigAddingService);
          MySqlServiceControlManager.Add(Settings.ServiceName, Settings.ServiceName, cmd, Settings.ServiceAccountUsername, Settings.ServiceAccountPassword, Settings.ServiceStartAtStartup);
          ReportStatus(Resources.ServerConfigServiceAdded);
        }
      }

      CurrentStep.Status = ConfigurationStepStatus.Finished;
    }