Follow me on Twitter

First time here?

Check out the Archive, and Subscribe to the RSS feed.

Adding an Application Pool to IIS7 programmatically

This post was written on April 16, 2008 19:21 by MartinHN

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.

image

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;
   6:   
   7:  namespace Iis7ManagementTest
   8:  {
   9:    class Program
  10:    {
  11:      static void Main(string[] args)
  12:      {
  13:        ServerManager mgr = new ServerManager();
  14:   
  15:        // Add a new application pool called MyAppPool
  16:        ApplicationPool myAppPool = mgr.ApplicationPools.Add("MyAppPool");
  17:        
  18:        // Configure my new app pool to start automatically.
  19:        myAppPool.AutoStart = true;
  20:   
  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;
  24:   
  25:        // Use the Integrated Pipeline mode
  26:        myAppPool.ManagedPipelineMode = ManagedPipelineMode.Integrated;
  27:   
  28:        // Set the runtime version of ASP.NET to 2.0
  29:        myAppPool.ManagedRuntimeVersion = "V2.0";
  30:   
  31:        // Use the Network Service account
  32:        myAppPool.ProcessModel.IdentityType = ProcessModelIdentityType.NetworkService;
  33:        
  34:        // Shut down after being idle for 5 minutes.
  35:        myAppPool.ProcessModel.IdleTimeout = TimeSpan.FromMinutes(5);
  36:   
  37:        // Max. number of IIS worker processes (W3WP.EXE)
  38:        myAppPool.ProcessModel.MaxProcesses = 1;
  39:   
  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:

image

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 DotNetKicks.com

Comments

February 22. 2008 21:22

trackback

Trackback from DotNetKicks.com

Adding an Application Pool to IIS7 programmatically

DotNetKicks.com

July 31. 2009 18:33

basem

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

basem

July 31. 2009 18:56

MartinHN

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.

MartinHN

September 24. 2009 05:09

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.

Ross

Ross Stephens

November 12. 2009 23:00

fast payday loans

Just try to smile for about 2-3 mins then you can get back to work

fast payday loans

November 14. 2009 23:03

SNooker Table Size

Thanks for sharing. I'm a novice at this type of stuff so this kind of help is invaluable.

SNooker Table Size

November 18. 2009 08:58

payday loans

I always wanted to write in my site something like that but I guess you'r faster Smile

payday loans

November 18. 2009 14:20

paydayloans

Nice resource. rss feed added

paydayloans

November 22. 2009 10:31

payday loans

Like your writing! Still you can do some things to improve it.

payday loans

November 22. 2009 21:08

payday loans

Like your writing! Still you can do some things to improve it.

payday loans

December 1. 2009 08:02

paydayloans

Interesting ... as always - is your blog making any cash advance ? ;)

paydayloans

January 5. 2010 19:05

cash loans

thanks!  very helpful post!! like the template btw ;)

cash loans

January 10. 2010 11:47

Hirephpdevelopers

Nice information about application pool to IIS programmatically and also given coding details.

Hirephpdevelopers

January 10. 2010 13:08

payday loans

I guess there's always an easier way ...

payday loans

January 24. 2010 14:10

pay day loans

To do something, however small, to make others happier and better, is the highest ambition, the most elevating hope, which can inspire a human being.

pay day loans

February 9. 2010 03:41

Pool Repair

I'm searching for a tutorial in repairing a pool and com up to this. Anyway, thanks for sharing this information but I really have a hard time understanding different languages in web design.

Pool Repair

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.5.1.24

About the author

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in  anyway.

© Copyright 2009