Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a managed API to manage IIS 8?

Tags:

c#

.net-4.5

iis-8

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.

like image 654
Sean Airey Avatar asked Jun 13 '13 12:06

Sean Airey


1 Answers

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

like image 79
Maverik Avatar answered Nov 15 '22 20:11

Maverik