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