12

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.

Sean Airey
  • 6,352
  • 1
  • 20
  • 38

2 Answers2

4

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

Maverik
  • 5,619
  • 35
  • 48
  • I just ran it actually on IIS instead of debugging from visual studio and the code works fine, but it doesn't actually stop the site.. – Sean Airey Jun 13 '13 at 14:15
  • I had to use a named principal for Application Pool Identity with administrative access to get this working in IIS. Default won't work. Of course don't end up mixing the two dlls. They only work on their own IIS versions. nuget doesn't have 7.9.0.0 needed to manage IIS 8 right now as I write this so you have to resort to GAC for this. – Maverik Jun 13 '13 at 16:58
  • -1. The NuGet package is not official and it might not match the IIS installation on your machine. The GAC version should always be used and that's the only Microsoft supported approach. – Lex Li May 04 '14 at 05:05
  • 1
    @LexLi -1 doesn't make sense when I've merely presented proof of both things and the question isn't about what is the preferred version. I don't see anywhere me endorsing one over the other and if you want to make a comment, do that only please. – Maverik May 06 '14 at 10:17
  • If you remove the NuGet part, I can consider removing the down vote. You not only presented proof, but also might mislead readers. – Lex Li May 06 '14 at 12:50
  • @LexLi I make no mention of taking one route over the other. I guess we'll just agree to disagree and I'll take the downvote. Edit: Another reason for proof is the chat the happened before I made this answer and in there, OP had tried nuget which is what i was proving in the answer. But yea, if you feel the answer deserves the downvote instead of a simple recommendation comment; then that's what it is I guess. – Maverik May 06 '14 at 15:17
0

Ensure you have "impersonate" turned on and you are assigning the User and Password of the impersonate statement in the web.config with a correct administrators information. This will alleviate any further issue.

In the web.config add this statement if it is not their already.

<identity impersonate="true" userName="LOCALMACHINE\ADMINUSER" password="PASSWORD" />

Also ensure in the IISManager that you have turned impersonation ON "enabled" and Anonymous authentication OFF "disabled".