Search code examples
c#.netwindowsserviceservicecontroller

Can't access ServiceController (InvalidOperationException) when trying to read on remote/another computer


I have a local service running on my computer and trying to get other computers to be able to read the status of my service (whether it's running, stopped, etc.) However, I am unable to as I get an InvalidOperationException error, saying that I am unable to open Service Control Manager. Locally, I am able to, but on another remote computer I am unable to. The ServiceController (cs) object just returns an object with properties that all have the InvalidOperationException error.

I've tried closing down all the firewalls on the other computers, tried running Visual Studio on Administrator privileges, but nothing seems to be working. I've noticed that others suggested hard coding your admin credentials and using WindowsIdentity and Impersonation but that wouldn't work for my project (as it wouldn't be a viable solution at my workplace - wouldn't make sense with the business logic as don't want to give clients any in-house credentials).

Here's my snippet of code:

    public bool CheckServiceStatus()
    {
        try
        {
            string machineName = pubSubConfig.MachineName;
            string serviceName = pubSubConfig.ServiceName;

            System.ServiceProcess.ServiceController cs = new System.ServiceProcess.ServiceController(serviceName, machineName);

            if (cs != null && cs.ServiceName == serviceName && cs.Status == System.ServiceProcess.ServiceControllerStatus.Running)
            {
                return true;
            }                
        }
        catch(Exception ex)
        {
            Trace.TraceError("Unable to check service status: /r/n {0}", ex.Message);
        }
        return false;
    }

The error is this:

System.InvalidOperationException: Cannot open Service Control Manager on computer '___'. 
This operation might require other privileges. ---> System.ComponentModel.Win32Exception: Access is denied

Does anyone know any workarounds as to how I can get other computers running my C# program to be able to read the ServiceController object?

Thanks!


Solution

  • So as I said in the comments, I was not able to get around the SCM (Service Control Manager) due to admin privileges when accessing another remote computer (makes sense if you think about security). However, I did find another solution that was more or less a workaround. I'll post the solution here in case anyone finds it helpful.

    So to check the status of the Windows Service (like if it's running or not), I added an additional WCF service that is hosted in the Windows Service. So now the service can expose a method that literally just returns true.

    Essentially the thought-process around it was that if the WCF service is accessible then that means the Window Service is running, and thus will always return true. If the Windows service is down, the WCF service will also be down and thus making that method not available. You wouldn't get anything to return, so you would know that the service is down and not running.

    Hope that helps someone! I know it's not really a direct solution to the problem I had originally asked, but it was a workaround, indirect solution.