Adding an Application Pool to IIS7 programmatically

Internet Information Services 7 (IIS7) has a great new set of features regarding configuration. Using the Integrated Configuration system, you can configure your server from XML files, the IIS7 manager, the command prompt using the APPCMD tool, but IIS7 also lets you manage your server from managed code in a very intuitive manner.

If you navigate to the %WINDIR%\System32\InetSrv folder in Windows Vista, you’ll find all the executables, DLL’s and XML (.config) configuration files you need. It doesn’t matter if you use the IIS7 Manager, managed code or the APPCMD command-line based tool to manage your server – at the end of the day you are changing an XML file. That is applicationHost.config which is located here: C:\Windows\System32\inetsrv\config\applicationHost.config.

The integrated configuration system on IIS7 is great news for hosters, and web developers. Hosters can easily automate server management through managed code, and as a web developer, you can configure your server from your web.config file, which makes it easy to move your web application from development, to test, and further up towards production. Read my post on how to set a websites default document, from within the web application’s web.config file.

In this post, I will create an application pool on my local IIS7.

Launch Visual Studio 2005 or 2008 – whatever you’ve got will work.

Create a new Console Application, and give it a name of your own choice.

Right click References in the Solution Explorer and add a new reference.


Locate Microsoft.Web.Administration.dll from the C:\Windows\System32\inetsrv folder.

To access IIS7, we use the ServerManager class, as shown below.

 1: using System;
 2: using System.Collections.Generic;
 3: using System.Linq;
 4: using System.Text;
 5: using Microsoft.Web.Administration;
 7: namespace Iis7ManagementTest
 8: {
 9:   class Program
 10:   {
 11:     static void Main(string[] args)
 12:     {
 13:       ServerManager mgr = new ServerManager();
 15:       // Add a new application pool called MyAppPool
 16:       ApplicationPool myAppPool = mgr.ApplicationPools.Add("MyAppPool");
 18:       // Configure my new app pool to start automatically.
 19:       myAppPool.AutoStart = true;
 21:       // What action should IIS take when my app pool exceeds 
 22:       // the CPU limit specified by the Limit property
 23:       myAppPool.Cpu.Action = ProcessorAction.KillW3wp;
 25:       // Use the Integrated Pipeline mode
 26:       myAppPool.ManagedPipelineMode = ManagedPipelineMode.Integrated;
 28:       // Set the runtime version of ASP.NET to 2.0
 29:       myAppPool.ManagedRuntimeVersion = "v2.0";
 31:       // Use the Network Service account
 32:       myAppPool.ProcessModel.IdentityType = ProcessModelIdentityType.NetworkService;
 34:       // Shut down after being idle for 5 minutes.
 35:       myAppPool.ProcessModel.IdleTimeout = TimeSpan.FromMinutes(5);
 37:       // Max. number of IIS worker processes (W3WP.EXE)
 38:       myAppPool.ProcessModel.MaxProcesses = 1;
 40:       // Commit the changes
 41:       mgr.CommitChanges();
 42:     }
 43:   }
 44: }

After the application has been executed, we will see our new application pool inside the IIS7 Manager:


That’s all for now. In another blog post I will show how to add a new web site, that will use our new application pool – also from managed code.

kick it on


The Integrated Programming model in IIS 7

IIS 7 comes with a bunch of improvements for developers. You can do a whole lot of exciting things even from web.config, but also from code.

To take advantage of the powerful integrated programming model, you need to set your application pool to use the Integrated pipeline mode. There’s no limit to what you can do.

I found a lot of great stuff in the book called Professional IIS 7 and ASP.NET Integrated Programming and learned a lot of useful stuff.

IIS 7 (or 7.5) is still the preferred way to develop, test and deploy ASP.NET applications, and as a developer you must stay up-to-date on what your tools and framework has to offer!

  • Martin H. Normark

    Hi Basem

    You should be able to find it in the IIS directory (%WinDir%\System32\InetSrv).

    Make sure that the IIS Management Script and Tools is installed. It is located in the "Turn Windows features on or off" dialog, under Internet Information Services.

  • basem

    Yes, I like to change settings from default app pool to classic, I dont have microsoft.web.admin dll. I tried to download package, I don’t see it any where. I have Vista Home premium.

  • Ross Stephens

    Microsoft.Web.Administration in VS2005 on XP

    Do you know whether it is possible to include Microsoft.Web.Administration when developing in VS 2005 on XP ?

    Thanks in advance.


  • Hoang

    myAppPool.ManagedRuntimeVersion = “V2.0″; <= I've got problem with the "V", it should be "v"

  • Martin H. Normark

    @9045edb59645cfe6b3fd2eb3f63716e6:disqus You’re right. I’ve updated the post.

  • victorhdzhdz

    Hello Martin

    I’m trying to make an application pool from c# code, I have tried using the DirectoryEntry class and the ApplicationPool class without luck

    Looks like I don’t have some kind of privileges the IIS_USRS because when I run the program sends me access denied, do you have any idea? am I missing something else?


  • Martin H. Normark

    Are you running the code from an ASP.NET web app?

  • victorhdzhdz

    Hi Martin, thanks for answer me back, in fact, I already have the solution. The solution was a little change in the application pool Identity attribute, change ApplicationPoolIdentity for Local System and it’s works!

  • Martin H. Normark

    Ok, cool!