Restart a service with dependent services?

后端 未结 3 1979
广开言路
广开言路 2021-02-20 07:36

Starting with a csharp-example and duly noting related SO questions ( Restart a windows services from C# and Cannot restart a Service) and various other questions relating to

相关标签:
3条回答
  • 2021-02-20 07:46

    It sounds like you want to restart a "base" service and have all the things that rely on it get restarted as well. If so, you can't just restart all the dependant services, because they may not have been running beforehand. There's no API for this that I'm aware of.

    The way I'd do it is just write a recursive function to scan all the dependant services (and their dependancies), and add all the services which were running to a list in order.

    When you restart the base service, you can then just run through this list and start everything. Provided you haven't re-sorted the list, the services should start in the right order, and all will be well.

    0 讨论(0)
  • 2021-02-20 07:50

    Please notice that ServiceController.Stop() stops 'dependent' services and ServiceController.Start() starts 'dependent on' services - thus after stopping the service you'll only need to start services that are dependency tree's leaves.

    Assuming no cyclic dependencies are allowed, the following code gets services that need to be started:

        private static void FillDependencyTreeLeaves(ServiceController controller, List<ServiceController> controllers)
        {
            bool dependencyAdded = false;
            foreach (ServiceController dependency in controller.DependentServices)
            {
                ServiceControllerStatus status = dependency.Status;
                // add only those that are actually running
                if (status != ServiceControllerStatus.Stopped && status != ServiceControllerStatus.StopPending)
                {
                    dependencyAdded = true;
                    FillDependencyTreeLeaves(dependency, controllers);
                }
            }
            // if no dependency has been added, the service is dependency tree's leaf
            if (!dependencyAdded && !controllers.Contains(controller))
            {
                controllers.Add(controller);
            }
        }
    

    And with a simple method (e.g. extension method):

        public static void Restart(this ServiceController controller)
        {
            List<ServiceController> dependencies = new List<ServiceController>();
            FillDependencyTreeLeaves(controller, dependencies);
            controller.Stop();
            controller.WaitForStatus(ServiceControllerStatus.Stopped);
            foreach (ServiceController dependency in dependencies)
            {
                dependency.Start();
                dependency.WaitForStatus(ServiceControllerStatus.Running);
            }
        }
    

    You can simply restart a service:

        using (ServiceController controller = new ServiceController("winmgmt"))
        {
            controller.Restart();
        }
    

    Points of interest:

    For code clearity I didn't add:

    • timeouts
    • error-checking

    Please notice, that the application may end up in a strange state, when some services are restarted and some are not...

    0 讨论(0)
  • 2021-02-20 08:06

    Alright, finally implemented this. I've posted it as a separate answer as I had already come to this conclusion in the original update to my question, which was posted prior to the first answer.

    Again, the StartService(), StopService() and RestartService() methods follow the conventions outlined in the examples and such already referenced in the question itself (i.e. they wrap Start/Stop behavior to avoid "already started/stopped"-type exceptions) with the addendum that if a Service is passed in (as is the case below), Refresh() is called on that service before checking its Status.

    public static void RestartServiceWithDependents(ServiceController service, TimeSpan timeout)
    {
        int tickCount1 = Environment.TickCount; // record when the task started
    
        // Get a list of all services that depend on this one (including nested
        //  dependencies)
        ServiceController[] dependentServices = service.DependentServices;
    
        // Restart the base service - will stop dependent services first
        RestartService(service, timeout);
    
        // Restore dependent services to their previous state - works because no
        //  Refresh() has taken place on this collection, so while the dependent
        //  services themselves may have been stopped in the meantime, their
        //  previous state is preserved in the collection.
        foreach (ServiceController dependentService in dependentServices)
        {
            // record when the previous task "ended"
            int tickCount2 = Environment.TickCount;
            // update remaining timeout
            timeout.Subtract(TimeSpan.FromMilliseconds(tickCount2 - tickCount1));
            // update task start time
            tickCount1 = tickCount2;
            switch (dependentService.Status)
            {
                case ServiceControllerStatus.Stopped:
                case ServiceControllerStatus.StopPending:
                    // This Stop/StopPending section isn't really necessary in this
                    //  case as it doesn't *do* anything, but it's included for
                    //  completeness & to make the code easier to understand...
                    break;
                case ServiceControllerStatus.Running:
                case ServiceControllerStatus.StartPending:
                    StartService(dependentService, timeout);
                    break;
                case ServiceControllerStatus.Paused:
                case ServiceControllerStatus.PausePending:
                    StartService(dependentService, timeout);
                    // I don't "wait" here for pause, but you can if you want to...
                    dependentService.Pause();
                    break;
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题