2

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?

Avilan
  • 608
  • 2
  • 6
  • 23
  • Additionally, starting the service with NET START from the command line results in the same exception being thrown. – Avilan Nov 08 '11 at 13:11
  • What is the identity of the service? Does it run as a user account or service account? Is machine in the domain? – seva titov Nov 08 '11 at 15:17

2 Answers2

1

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".

Avilan
  • 608
  • 2
  • 6
  • 23
0

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.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
  • If the RPC service wasnt started, then the WBEMTest would not have worked either. – Avilan Nov 08 '11 at 13:01
  • Are you connecting with WBEMTest once your machine is fully booted? Or from where? Services start even before anyone logs into Windows, by the time you're logged on, RPC and WBEMTest would be available. – Grant Thomas Nov 08 '11 at 13:03
  • My service is installed by an installer, and thereafter started with ServiceController. At this point the computer is already fully booted as well. – Avilan Nov 08 '11 at 13:08
  • OK - and what if you reboot post-install, does the service start, run again but without the error? If a restart is irrelevant due to a fully loaded environment, then this answer may be too - but I'll leave it for future reference regardless. – Grant Thomas Nov 08 '11 at 13:10
  • Yes, both a reboot and starting it manually from Services.msc makes it run without errors. But I do not want to require a reboot. – Avilan Nov 08 '11 at 13:13
  • I wouldn't want to have to reboot either, it was just a curiosity. I can't imagine that this is a privileges error, though, since the service would be running independently of the user _and_ under the same account regardless of how it is started.. – Grant Thomas Nov 08 '11 at 13:17