Windows service started with ServiceController throws “RPC Server not available” when performing WMI connection

StackOverflow https://stackoverflow.com/questions/8050647

Question

I have a Windows service being installed with my installer, and then started with the use of ServiceController:

public static int StartService(string serviceName, int timeoutMilliseconds)
{
    ServiceController service = new ServiceController(serviceName);
    try
    {
        TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

        service.Start();
        service.WaitForStatus(ServiceControllerStatus.Running, timeout);

        service.Close();

        return 0;
     }
     catch
     {
        return 1;
     }
}

The service seems to start just fine, but when the service tries to perform WMI calls to remote computers, it throws an exception;

The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)

I've tried connecting with WBEMTest from the machine the service is running on, to the same machine the service tries to connect to, and it works fine.

Also, if I start the service manually from Services.msc, it works perfectly. What am I missing with ServiceController?

Était-ce utile?

La solution

I've figured it out.

When configuring the .config file of the service I use placeholders like [UserName] and [Password] to replace actual values given by the user in the installer.

The service got started before these values were swapped out, and the service tried connecting with the username and password as [UserName] and [Password].

I didnt think of this possibility at first because I thought I would get an "Access is denied" error, but for some reason when the username contains [ or ] the connection returns "RPC server not available".

Autres conseils

I'd bet on the required services (RPC) not yet being started prior to calls.

That is to say, your service must start, or at least only start processing, when it has the means to do so, which would be when the RPC service is started. Any calls dependent on RPC made prior to RPC starting will result in failure.

Dependency information is stored in the registry, to my knowledge; you can deploy a registry script with your solution and run it upon installation.

So, for instance, you will need to create a value at the following location:

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\<Service>

Which stores the registry key names of the services on which your own depends.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top