0

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!

Tim
  • 41
  • 8
  • What have you tried? https://www.google.com.au/search?q=Access+is+denied+remote+services – mjwills Oct 25 '17 at 20:49
  • I've only tried turning off firewalls and running Visual Studio in admin mode on the other computer to access the service on my local. All had the same result - my ServiceController object had all properties that were InvalidOperationException errors. – Tim Oct 25 '17 at 21:00
  • I'd recommend reading that google link and giving some of the suggestions there a try. – mjwills Oct 25 '17 at 21:01
  • Similarly to others, my program will be used on several other computers across the network. Thus, I'm not really inclined to add them all onto the Win34 admin group or use Impersonation and give my user/password inside the code. I mean, that would have to be a last resort I guess. – Tim Oct 25 '17 at 21:03
  • To anyone who wants an update... To be honest, there wasn't really a workaround as SCM (Service Control Manager) requires admin privileges when accessing another remote computer. So instead of checking the status of the Windows Service, I ended up adding an additional WCF service that is hosted in the Windows Service. What that, I added a method called CheckServiceStatus that literally returns a true. Now on my client side, I add a service reference to WCF service and call that method. It'll always return true if the service is running, throw exception if not running (and return false). – Tim Nov 02 '17 at 21:51

1 Answers1

0

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.

Tim
  • 41
  • 8