简体   繁体   中英

C# Cancelling ServiceController Service State Change Request

I've not been able to find any information on this, so I apologize if this is a duplicate.

I'm using an ASP website to trigger a WebMethod on my code behind that will trigger the service on the computer to change its state to some state specified on the front-end.

However, I want to make it so that when I call ServiceController.WaitForStatus() on Timeout it returns to the default status it started with prior to the call. So, if something doesn't start/pause/stop fast enough it'll revert to its previous state like nothing happened.

Is there a way to do this? I tried calling ServiceController.close() and that did nothing for me.

Is there a way to manage this gracefully so that if the timeout exception occurs it catches the exception gracefully and returns the service back to its original state prior to the service controller call.

This is the web method code I'm using:

[WebMethod]
    public static string ToggleService(string serverName, string serviceName, string newServiceState) {
        ServiceController serviceToToggle = null;
        if (newServiceState.ToLower().Equals("start")
            || newServiceState.ToLower().Equals("pause")
            || newServiceState.ToLower().Equals("stop")) {
            serviceToToggle = new ServiceController(serviceName, serverName);
        }

        if (serviceToToggle != null)
        {
            try
            {
                if (serviceToToggle.Status == ServiceControllerStatus.Paused)
                {
                    if (newServiceState.ToLower().Equals("start"))
                    {
                        serviceToToggle.Start();

                        serviceToToggle.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 1));
                    }
                    else if (newServiceState.ToLower().Equals("stop"))
                    {
                        serviceToToggle.Stop();
                        serviceToToggle.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 1));
                    }
                }
                else if (serviceToToggle.Status == ServiceControllerStatus.Running)
                {
                    if (newServiceState.ToLower().Equals("pause"))
                    {
                        serviceToToggle.Pause();
                        serviceToToggle.WaitForStatus(ServiceControllerStatus.Paused, new TimeSpan(0, 0, 1));
                    }
                    else if (newServiceState.ToLower().Equals("stop"))
                    {
                        serviceToToggle.Stop();
                        serviceToToggle.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 1));
                    }
                }
                else if (serviceToToggle.Status == ServiceControllerStatus.Stopped)
                {
                    if (newServiceState.ToLower().Equals("start"))
                    {
                        serviceToToggle.Start();
                        serviceToToggle.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 1));
                    }
                }

                return "\"OutputStatus\": \"Success\"";
            }
            catch (Exception e) {
                serviceToToggle.Close();
                return "\"OutputStatus\": \"Failed\"";
            }
        }

        return "No service to toggle";
    }

I would appreciate any help or insight into this.

In short, no - there is no way to revert or cancel a command already sent to a service.

You could write code to remember the previous state, and initiate a controller request to set that state again if something goes awry or times out, but it doesn't sound particularly robust.

If they're your own services, it's better to write them to properly respond to service requests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM