Reputation: 21962
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
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