简体   繁体   中英

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?

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).

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