In IIS7, you used to be able to use the Microsoft.Web.Administration
dll to manage IIS.
I have added this reference to my project, however running the following code results in a NotImplementedException
at site.Stop()
:
using (var server = new ServerManager())
{
var site = server.Sites.FirstOrDefault(s => s.Name == instanceName);
if (site != null)
{
site.Stop();
}
}
Is there an updated version of this API or an alternate method to manage IIS from .Net?
I would prefer not to use WMI or have to spawn an instance of appcmd
if at all possible.
I just tested the following snippet on both IIS 8 and 7 (using 7.9.0.0 from GAC of Windows 8 & 7.0.0.0 from nuget for a 2008 R2 machine respectively)
and I have no problem stopping the site:
var manager = new ServerManager();
manager.Sites[0].Stop();
manager.Dispose();
The only thing I had to do special was run Linqpad as Administrator explicitly to get this to work. Perhaps that's your issue? Windows 8 / Server 2012 do not give you Administrator access automatically unless your application manifest mandates it. I believe this holds for 7 / 2008 R2 as well but irrelevant since you've explicitly tagged for IIS8 (UAC ftw!)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With