Note that there are some explanatory texts on larger screens.

plurals
  1. PORestart a service with dependent services?
    primarykey
    data
    text
    <p>Starting with a <a href="http://www.csharp-examples.net/restart-windows-service/" rel="nofollow noreferrer">csharp-example</a> and duly noting related SO questions ( <a href="https://stackoverflow.com/questions/3312656/restart-a-windows-services-from-c">Restart a windows services from C#</a> and <a href="https://stackoverflow.com/questions/3309990/cannot-restart-a-service-c/">Cannot restart a Service</a>) and various other questions relating to <em>restarting just one service</em>, I'm wondering what the best method is for <em>restarting a service <strong>with dependent services</strong></em> (e.g. <code>Message Queuing</code>, on which <code>Message Queuing Triggers</code> depends, or <code>IIS</code>, on which <code>FTP Publishing</code> and <code>World Wide Web Publishing</code> depend). The mmc snap-in does this automagically, but the code doesn't seem to provide the same functionality (at least not as easily).</p> <p><a href="http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller.stop.aspx" rel="nofollow noreferrer">MSDN documentation for Stop</a> says "If any services depend on this service for their operation, they will be stopped before this service is stopped. The DependentServices property contains the set of services that depend on this one," and <a href="http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller.dependentservices.aspx" rel="nofollow noreferrer"><code>DependentServices</code></a> returns an array of services. Assuming <code>StartService()</code> and <code>StopService()</code> follow the conventions outlined in the examples and such referenced above (except that they accept <code>ServiceControllers</code> and <code>TimeSpans</code> directly), I started with:</p> <pre><code>public static void RestartServiceWithDependents(ServiceController service, TimeSpan timeout) { ServiceController[] dependentServices = service.DependentServices; RestartService(service, timeout); // will stop dependent services, see note below* about timeout... foreach (ServiceController dependentService in dependentServices) { StartService(dependentService, timeout); } } </code></pre> <p>But what if the service dependencies are nested (recursive) or cyclical (if that's even possible...) - if <code>Service A</code> is <em>depended on</em> by <code>Service B1</code> and <code>Service B2</code> and <code>Service C1</code> <em>depends on</em> <code>Service B1</code>, it seems 'restarting' <code>Service A</code> by this method would stop <code>Service C1</code> but wouldn't restart it...</p> <p>To make this example picture clearer, I'll follow the model in the services mmc snap-in:</p> <pre><code>The following system components depend on [Service A]: - Service B1 - Service C1 - Service B2 </code></pre> <p>Is there a better way to go about this, or would it just have to recursively step into and stop each dependent service and then restart them all after it restarts the main service?</p> <p>Additionally, will <em>dependent</em> but <em>currently stopped</em> services be listed under DependentServices? If so, wouldn't this restart them anyways? If so, should we control that as well? This just seems to get messier and messier...</p> <p>*Note: I realize the <code>timeout</code> isn't being applied completely correctly here (overall timeout could be many many times longer than expected), but for now that's not the issue I'm concerned about - if you want to fix it, fine, but don't just say 'timeout's broken...'</p> <p><strong>Update:</strong> After some preliminary testing, I've discovered (/confirmed) the following behaviors:</p> <ul> <li>Stopping a service (e.g. <code>Service A</code>) that other services (e.g. <code>Service B1</code>) depend on will stop the other services (including "nested" dependencies such as <code>Service C1</code>)</li> <li><a href="http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller.dependentservices.aspx" rel="nofollow noreferrer"><code>DependentServices</code></a> <em>does</em> include dependent services in all states (Running, Stopped, etc.), and it also includes nested dependencies, i.e. <code>Service_A.DependentServices</code> would contain <code>{Service B1, Service C1, Service B2}</code> (in that order, as <code>C1</code> depends on <code>B1</code>).</li> <li>Starting a service that depends on others (e.g. <code>Service B1</code> <em>depends on</em> <code>Service A</code>) will also start the requisite services.</li> </ul> <p>The code above can therefore be simplified (at least in part) to just stop the main service (which will stop all dependent services) and then restarting the most-dependent services (e.g. <code>Service C1</code> and <code>Service B2</code>) (or just restarting "all" the dependent services - it will skip the ones already started), but that really just defers the starting of the main service momentarily until one of the dependencies complain about it, so that doesn't really help.</p> <p>Looks for now like just restarting all the dependencies is the simplest way, but that ignores (for now) managing services that are already stopped and such...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload