Brian Deacon
Brian Deacon

Reputation: 21962

What is the equivalent of an IISReset if I want to use Microsoft.Web.Administration

It would appear that there is more involved than just iterating the ApplicationPools and calling Recycle() on them. How do I most closely mimic an IISReset programatically (from .Net) without resorting to shell invocation?

And I suppose most importantly: Are there things that IISReset does that can't be accomplished via the tools available in Microsoft.Web.Administration namespace?

Upvotes: 3

Views: 908

Answers (1)

JohnnyFun
JohnnyFun

Reputation: 4293

You can use System.ServiceProcess.ServiceController (got the idea from this answer), by starting or stopping the following services:

-Windows Process Activation Service (WAS)
-World Wide Web Publishing Service (W3SVC)

Here's some code that'll do the job:

//stop iis (like running "IISReset /Stop")
ResetService("WAS", false);
ResetService("W3SVC", false);

//start iis (like running "IISReset /Start")
ResetService("WAS", true);
ResetService("W3SVC", true);

private static void ResetService(string name, bool start)
{
    using (var service = new System.ServiceProcess.ServiceController(name))
    {
        if (start && service.Status == ServiceControllerStatus.Stopped)
        {
            service.Start();
        }
        else if (!start && service.Status == ServiceControllerStatus.Running)
        {
            service.Stop();
        }
    }
 }

As for other IISReset commands, you could code in a timeout easily enough. And to reboot the computer, check out this answer. Let me know if you need any more details.

But if that's not enough of a party for ye, you can always execute power shell scripts in c#, using this technique (pretty baller, if you just need to get 'er done).

Upvotes: 1

Related Questions