Microsoft.Web.Administration in IIS 7

While creating the new administration stack in IIS 7, we were looking into the different ways users could manipulate the server configuration as well as the new runtime information available in IIS 7 (Internally we call this RSCA-Runtime State and Control API) from managed code, and we realized we needed to provide a simpler and more straight forward API that developers could consume from managed code. Microsoft.Web.Administration is the answer to this problem. This API is designed to be simple to code against in an “intellisense-driven” sort of way. At the root level a class called ServerManager exposes all the functionality you will need.

To show the power and simplicity of this API, let’s look at some samples below. To try this samples just create a new Console Application in Visual Studio and add a reference to Microsoft.Web.Administration.dll that can be found at IIS directory (%WinDir%\System32\InetSrv).
Please note that the following code is based on Windows Vista Beta 2 code and will likely change for the release candidate versions of Windows Vista since we have planned several enhancements to simplify the API and expose more features into it.
The following picture shows the main objects (excluding Configuration related classes).
Creating a Site

ServerManager iisManager = new ServerManager();

This basically creates an instance of the ServerManager class and uses the Add method in the Sites collection to create a new site named “NewSite” listening at port 8080 using http protocol and content files are at d:\MySite.
One thing to note is that calling Update is a requirement since that is the moment when we persist the changes to the configuration store.
After running this code you have now a site that you can browse using http://localhost:8080

Adding an Application to a site

ServerManager iisManager = new ServerManager();

This sample uses the Sites collection Indexer to get NewSite site and uses the Applications collection to add a new http://localhost:8080/Sales application.

Creating a Virtual Directory

ServerManager iisManager = new ServerManager();
Application app iisManager.Sites[“NewSite”].Applications[“/Sales”];

Runtime State and Control

Now, moving on to the new Runtime state and control information we also expose in this objects information about their current state as well as the ability to modify them. For example, we expose the list of W3WP processes running (Worker processes) and what I think is really cool, we even expose the list of requests currently running. Stopping a Web Site

ServerManager iisManager = new ServerManager();

Recyciling an Application Pool

ServerManager iisManager = new ServerManager();

Getting the list of executing requests

ServerManager iisManager = new ServerManager();
(WorkerProcess w3wp in iisManager.WorkerProcesses) {
“W3WP ({0})”, w3wp.ProcessId);
(Request request in w3wp.GetRequests(0)) {
“{0} – {1},{2},{3}”,

Another big thing on this API is the ability to edit the “.config” files using a simple API, this includes the ability of modifying the main applicationHost.config file from IIS, web.config files from as well as machine.config and other config files (such as administration.config). However I will talk about them in a future post.

Comments (61)

  1. Anonymous says:

    That might possibly be the coolest thing I’ve ever seen IIS do.

  2. Anonymous says:

    Its Wonderful and very cool.

    special thanks to Mr. carlos, Microsoft and Microsoft communities.

  3. Anonymous says:

    Waauuu, we will be saving so many lines of code and using this extensive.

  4. Anonymous says:

    Does this modify the (what used to be) metabase? When dealing with several web servers in a cluster, will I still need to sync the metabases (or .config files) across nodes, or can I finally centralize all this on a NAS via UNC?

  5. Anonymous says:

    Mark – IIS7 does not use the metabase to store configuration. It is totally within .config files.

    There is a Legacy ABO Compatibility component (completely optional, but installed by default) which you can install to capture all legacy API calls to configure the metabase and transparently translate it into .config settings.

    You will still need to synchronize config settings across servers in a cluster, though if you run the same command remotely it’d do the same.

    I have yet to understand the need to have IIS read configuration from a UNC share vs ability to copy the same IIS configuration to multiple machines. They accomplish the same objective; you can view one as a local-cached version of the other. Central UNC share sounds cooler but is more fragile, so why???

    In other words, suppose you get what you want — the "centralized" config file on NAS via UNC. All that means is that after IIS calls CreateFile to get a handle to the file, read/write to the file can now RANDOMLY fail as IIS works with various sections of the file (and underneath the scenes, network traffic is getting used to fulfill the read/write).

    What should IIS do? Bail on first error? Bail after retrying 5 times in 5 seconds (but suppose you are modifying config — where do they go)? Cache the change locally and periodically propagate back to the UNC (but explain how this is any better than IIS reading local .config file and a separate "syncing" service periodically syncing changes between multiple locations?

    In other words — please explain your real scenario and requirements, not how you think we should implement things.


  6. Anonymous says:


    Say, for example, you have several IIS nodes behind a load balancer. Then, you expose a user-facing control panel application that modifies the metabase for a feature like Application Roots, for example. This requires the replication of the "master" metabase (the one where the control panel made the change) to all the "slave" IIS nodes. This can’t occur realtime because of the sheer volume of updates occurring to the metabase. So, you have to replicate on a schedule – say, every 5 minutes. David,

    Say, for example, you have several IIS nodes behind a load balancer. Then, you expose a user-facing control panel application that modifies the metabase for a feature like Application Roots, for example. This requires the replication of the "master" metabase (the one where the control panel made the change) to all the "slave" IIS nodes. This can’t occur realtime because of the sheer volume of updates occurring to the metabase. So, you have to replicate on a schedule – say, every 5 minutes.

    What a NAS/UNC based configuration store (metabase) buys you is a way out of the replication scenario. However, there still exists the random fail issue as you described. Ideally, I’d like to see a realtime replication method similar to Active Directory. Here, there are multiple peer nodes, where any change is replicated to the other nodes. Maybe this can be accomplished via a pure in-memory database with replication via two-phase commit across IIS nodes. Maybe a heartbeat service between IIS nodes? Just brainstorming here.

    – Mark

  7. Anonymous says:

    O Scott Guthrie, do time de produtos Web da Microsoft, escreveu um post bem interessante sobre as novidades…

  8. Anonymous says:

    Though IIS 7 still support WMI, ABO, etc administrative interface, but you will love this new managed…

  9. Anonymous says:

    Can you create AppPools with the new interface?

    I’ve been beating myselft up for a couple of months trying to figure out how to create an app pool via the WMI interface.  I’ve resorted to using ADSI, but I hear that’s going away…


  10. Anonymous says:

    How about adding a "re-read config files" function to allow central config storage and after editing the config, telling each machine to re-read the file to flush the servers internal cache?

  11. Anonymous says:

    Not a bad idea, considering change notification doesnt work on metabase.xml.

  12. Anonymous says:

    Прошли старые добрые деньки. Реестр закапывают в землю, бинарные форматы закапыв

  13. CarlosAg says:

    Yes you can create application pools as well as enumerate, remove them and even recycle them.

  14. Anonymous says:

    ну и слава богу, тока блин, когда еще смогу перетащить клиентов на 7ку…

  15. Anonymous says:

    Carlos Aguilar, del equipo de IIS 7 y desarrollador de la nueva consola y el nuevo API de administración…

  16. Anonymous says:

    Before the boom of web and internet, HTTP was not so common is everyday life. When internet became more…

  17. Anonymous says:

    Is there a way to Push data to client (Push Server ) using IIS 7? (ex live stock update !)

  18. Anonymous says:

    IIS7 is a major upgrade of IIS, and will ship in both Windows Vista as well as Windows Longhorn Server. …

  19. Anonymous says:

    ServerManager iisManager = new ServerManager();

    iisManager.Sites.Add("NewSite", "http", "*:8080:", "d:\MySite");

    >–>> iisManager.Update();

    I think .Update() method is replaces with .CommitChanges()

  20. Anonymous says:

    More than a year ago I wrote about Microsoft.Web.Administration.dll and how it was a new API we were

  21. Anonymous says:

    Can this be used to manage remote IIS servers within the same AD group?

    mick dot walker at gmail dot com

  22. CarlosAg says:

    Yes, you can use it to manage IIS 7 running in Windows Vista SP1 or Windows Server 2008 but only from a client running Windows Vista SP1 or Windows Server 2008. The way to do it is creating a ServerManager like this:

    ServerManager remoteManager = ServerManager.OpenRemote("yourServer");

  23. Anonymous says:

    More than a year ago I wrote about Microsoft.Web.Administration.dll and how it was a new API we were

  24. Anonymous says:

    Thanks for your reply Carlos.

    Do you know where I can find some ‘good’ documentation on this?

  25. Anonymous says:

    Also is there anyway to control FTP settings for a Site?

  26. Anonymous says:

    When using FTP 7.0 with IIS 7.0 and using Microsoft.Web.Administration how do add ftp settings to a web site? Or do I have to manually edit the .config files xml to add it?

    If not supported, would this feature be supported in future?

  27. CarlosAg says:

    You can absolutely use Microsoft.Web.Administration for that, however you will either need to use the loosely typed model (ConfigurationSection, ConfigurationElement, etc) or you will need to create your own strongly typed classes for it.

    For example if you want to set something in the site, you can use:

    ConfigurationElement ftpServer =ServerManager.Sites["Default Web Site"].GetChildElement("ftpServer");

    And then use that for anything you need, also FTP defines several sections as well that you can use GetSection() over a Configuration object.

  28. Anonymous says:

    IIS7伴随着Vista已经悄悄来临,学习的时候我也摘录了一些有关于此的文章。 不敢独享,还是贴出来大家共享吧!

  29. Anonymous says:

    Will this all be exposed to the (VB) scripting engine?

  30. Anonymous says:

    Hi Carlos,

    How do I query the current memory usage of a Site?



  31. Anonymous says:

    Интересную тему для вордпресса поставили. Сами делали или стандартную какую брали? 🙂

  32. Anonymous says:

    Hi Carlos ,

    I am facing that create a virtual director from C# code without having admin previliages the code is

               System.DirectoryServices.DirectoryEntry oDE;

               System.DirectoryServices.DirectoryEntries oDC;

               System.DirectoryServices.DirectoryEntry oVirDir;

               oVirDir = null;



                   // check whether to create FTP or IIS virtual directory

                   if (IsFTP)


                       oDE = new DirectoryEntry("IIS://" + deploymentServerName + "/MSFTPSVC/1/Root");




                       oDE = new DirectoryEntry("IIS://" + deploymentServerName + "/W3SVC/1/Root");




                       string directoryEntryNameStr = oDE.Name;


    now tell me a solution when I can create a virtual directory without being admin to windows Vista.

    I clicked IIS6.0 metadata and configuration compatibilty and manament from window feature on/off/

  33. CarlosAg says:

    For security reasons you cannot add a virtual directory unless you are an administrator.

    You can imagine that if a non-administrator was able to expose a random directory over the URL namespace of the site would be a bad idea.

  34. Anonymous says:



  35. Anonymous says:

    Can we use the Microsoft.Web.Administration from a Windows Server 2008 to remotely manage IIS 6 running on Windows Server 2003?  Where is the Microsoft.Web.Administration located?  Is it part of .Net or Windows?

  36. CarlosAg says:

    Unfortunately you can only use it to manage a Windows Vista, Windows Server 2008, Windows 7, and Windows Server 2008 R2 server.

    It is not distributed with .net but instead is part of IIS.

  37. Anonymous says:

    Are they any major changes to the Microsoft.Web.Administration API’s in IIS 7.5?

  38. Anonymous says:

    I am using VS2008 on an XP machine and cannot see the Microsoft.Web.Administration namespace. I guess this is because it is part of IIS7, which is not on the development machine. The finished code is to run on an IIS 7 server. I’d like to create a reference but don’t know where to find one. Can anyone help please?


  39. Anonymous says:

    Update: I installed the IIS Remote Manager on the XP dev machine thinking I might get the dll with it, but it appears not…

  40. Anonymous says:


  41. Anonymous says:


  42. Anonymous says:

    熟女だって性欲がある、貴方がもし人妻とSEXしてお金を稼ぎたいのなら、一度人妻ワイフをご利用ください。当サイトには全国各地からお金持ちのセレブたちが集まっています。女性から男性への報酬は、 最低15万円からと決めております。興味のある方は一度当サイト案内をご覧ください

  43. Anonymous says:

    Just wondering, How would one go about using this API to both View and Modify the contents of the IPGrant and/or Deny Table?

    Could you be so kind as to give an explanation or hint?

  44. CarlosAg says:

    @Philip, here is an example:

           using(ServerManager serverManager = new ServerManager()) {

               Configuration config = serverManager.GetApplicationHostConfiguration();

               ConfigurationSection ipSecuritySection = config.GetSection("system.webServer/security/ipSecurity");

               ConfigurationElementCollection ipSecurityCollection = ipSecuritySection.GetCollection();

               ConfigurationElement addElement = ipSecurityCollection.CreateElement("add");

               addElement["ipAddress"] = @"";





    You can also learn more at the configReference where we have samples for all sections:…/ipSecurity

  45. Anonymous says:

    using (ServerManager mgr = new ServerManager())


                           Site site = mgr.Sites[siteName];

                           if (site != null)


                               Microsoft.Web.Administration.Application app = site.Applications[appName];

                               if (app == null)


                                   Microsoft.Web.Administration.Application iisApplication =

                                   site.Applications.Add(appName, ApplicationPath);

                                   //sets the application to Classic .Net AppPool

                                   iisApplication.ApplicationPoolName = "ASP.NET v4.0 wcf";



  46. Anonymous says:

    anyone got a clue how to delete a virtual directory? with VirtualDirectories.Remove…. i cant get it to work

  47. Anonymous says:

    非常不错,但是最近我在开发一个使用IIS Express运行的控制本地系统自带的IIS的系统,问题出现了。



  48. Anonymous says:

    How do I get TARGETSITE (id for the website ) programmetically? Thx For example, get ID for MyWebSite?

  49. Anonymous says:

    Would love to be able to get code to delete 2 virtual directories/applications then check to see if application pool has any other apps assigned to it.  If not, delete the app Pool.

  50. Anonymous says:

    I'm trying to create a virtual directory using C#. When try to add a reference to Microsoft.Web.Administration Namespace I can't find it in the list. Any clue?

    And thanks for the great article.

  51. Anonymous says:

    How can  I using Microsoft.Web.Administration in my web site.

    I want create a web page to manager the iis.

  52. Anonymous says:

    How to assign a specific user credentials for a site in IIS 6.0 using Visual Studio help please

  53. Anonymous says:


  54. Anonymous says:

    its not working for remote administration, any code is really appritiated

  55. Anonymous says:

    Error On remote machine?

    Unauthorized Access Exception

  56. Tasin says:

    i want a vb code that can query the iis metadata to give the list of site that has browsing directory enabled status .can anyone provide it???

  57. Adrian says:

    Hi this is great, however I am sitting with a major headache and cannot for the love of me add the node


    to the file … do you perhaps have a solution? I had tried the below to no avail.

    Configuration config = serverManager.GetWebConfiguration(“Default Web Site”);
    ConfigurationSection applicationPoolsSection = config.GetSection(“system.web”);
    ConfigurationElementCollection applicationPoolsCollection = applicationPoolsSection.GetCollection();

    ConfigurationElement addElement = applicationPoolsCollection.CreateElement(“identity”);
    addElement[“key”] = “impersonate”;
    addElement[“value”] = “true”;


    1. CarlosAg says:

      You need to make sure request the right section, in this case it is system.web/identity (the whole thing), you can figure that out looking into %windir%\system32\inetsrv\config\schema

      So do:
      using(ServerManager serverManager = new ServerManager()) {
      Configuration config = serverManager.GetWebConfiguration(“Your Site”);

      ConfigurationSection identitySection = config.GetSection(“system.web/identity”);
      identitySection[“impersonate”] = true;