0

I'm trying to start a service in a custom action for my WiX Setupfor a project C#.

At first I check if service is started :

[CustomAction]
public static ActionResult StopService(Session session)
{
    ServiceController MyService = null;
    try
    {
        MyService = new ServiceController("MyService");
        if (MyService != null) &&(MyService.Status != ServiceControllerStatus.Stopped))
        {
            MyService.Stop();
            MyService.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 2, 0));
        }
        session.Log("Stop service");
    }
    catch (Exception ex)
    {
        session.Log(ex.ToString());
    }
}

But there is an Exception when I try to get the status (MyService.Status) :

System.InvalidOperationException: Impossible d'ouvrir le service MyService sur l'ordinateur '.'. ---> System.ComponentModel.Win32Exception: Le service spécifié n’existe pas en tant que service installé --- Fin de la trace de la pile d'exception interne --- à System.ServiceProcess.ServiceController.GetServiceHandle(Int32 desiredAccess) à System.ServiceProcess.ServiceController.GenerateStatus() à System.ServiceProcess.ServiceController.get_Status() à CustomAction.CustomActions.StopService(Session session)

Translation : "Unable to open service MyService on this computer -> Specified service doesn't not exist as installed service.

How can I check if a service is installed ? (I checked, MyService is not null)

A.Pissicat
  • 3,023
  • 4
  • 38
  • 93

1 Answers1

0

It means the specified service "MyService" is not installed on your computer. You can double check by going to Start > Run > services.msc.

Also instead of creating a direct object from serviceController, you can instead grab a list of installed services and search your service from that list:

    bool DoesServiceExist(string serviceName)
    {
       return ServiceController.GetServices().Any(serviceController => serviceController.ServiceName.Equals(serviceName));
    }

References: https://stackoverflow.com/a/23800234

Shawinder Sekhon
  • 1,569
  • 12
  • 23