Rick Strahl's Web Log

Wind, waves, code and everything in between...
ASP.NET • C# • HTML5 • JavaScript • AngularJs
Contact   •   Articles   •   Products   •   Support   •   Search
Ad-free experience sponsored by:
ASPOSE - the market leader of .NET and Java APIs for file formats – natively work with DOCX, XLSX, PPT, PDF, images and more

Programmatically listing, getting and setting Application Pools


:P
On this page:

I’m updating several of my Web Configuration utilities for various applications that provide post-installers or tools to otherwise set up Web sites. With Vista rolling around being able to select an application pool to assign the site to is becoming more important because in IIS7 the Application Pool determines the .NET version that is used as well as the pipeline mode (classic or the new integrated mode). So for setting up ASP.NET 2.0 applications typically you’ll want to select a classic pipeline application pool for the application so you can get easy debugging (debugging integrated mode with VS takes a little more work).

 

I’m updating this stuff specifically for my West Wind Web Store configuration utility as well as a standalone tool the Web Configuration Tool both of which need this functionality. Here’s what the Web Site Configuration Tool looks like:

 

 

In this case it’s using IIS 7 so the .NET version isn’t selectable as it’s selected as part of the Application Pool. For IIS 6 both .NET version and App Pool are available for IIS 5 only the .NET version is available.

 

So how do you get the ApplicationPools available, select and set one and create a new one? There are actually a number of ways (especially with IIS7) but the most widely supported by recent versions of IIS is still by using ADSI and DirectoryServices in .NET.

 

I talked about using ADSI for IIS with .NET a long while back with some examples of how to get virtuals and set properties etc. there.

 

So, here’s some additional code to deal with Application Pools in my WebConfiguration class (note there are a few depencies in this code, but you should be able to glean the general idea):

 

/// <summary>

/// Returns a list of all the Application Pools configured

/// </summary>

/// <returns></returns>

public ApplicationPool[] GetApplicationPools()

{           

    if (ServerType != WebServerTypes.IIS6 &&

        ServerType != WebServerTypes.IIS7)

        return null;

 

    DirectoryEntry root = this.GetDirectoryEntry("IIS://" + this.DomainName + "/W3SVC/AppPools");

      if (root == null)

            return null;

 

    List<ApplicationPool> Pools = new List<ApplicationPool>();

 

    foreach (DirectoryEntry Entry in root.Children)

    {

        PropertyCollection Properties = Entry.Properties;

 

        ApplicationPool Pool = new ApplicationPool();

        Pool.Name = Entry.Name;

       

        Pools.Add(Pool);

    }

 

    return Pools.ToArray();

}

 

/// <summary>

/// Create a new Application Pool and return an instance of the entry

/// </summary>

/// <param name="AppPoolName"></param>

/// <returns></returns>

public DirectoryEntry CreateApplicationPool(string AppPoolName)

{

    if (this.ServerType != WebServerTypes.IIS6 &&

        this.ServerType != WebServerTypes.IIS7)

        return null;

 

    DirectoryEntry root = this.GetDirectoryEntry("IIS://" + this.DomainName + "/W3SVC/AppPools");

    if (root == null)

        return null;

 

    DirectoryEntry AppPool = root.Invoke("Create","IIsApplicationPool",AppPoolName) as DirectoryEntry;           

    AppPool.CommitChanges();

 

    return AppPool;

}

 

 

/// <summary>

/// Returns an instance of an Application Pool

/// </summary>

/// <param name="AppPoolName"></param>

/// <returns></returns>

public DirectoryEntry GetApplicationPool(string AppPoolName)

{

    DirectoryEntry root = this.GetDirectoryEntry("IIS://" + this.DomainName + "/W3SVC/AppPools/" + AppPoolName);

    return root;

}

 

 

 

/// <summary>

/// Retrieves an Adsi Node by its path. Abstracted for error handling

/// </summary>

/// <param name="Path">the ADSI path to retrieve: IIS://localhost/w3svc/root</param>

/// <returns>node or null</returns>

private DirectoryEntry GetDirectoryEntry(string Path)

{

 

    DirectoryEntry root = null;

    try

    {

        root = new DirectoryEntry(Path);

    }

    catch

    {

        this.SetError("Couldn't access node");

        return null;

    }

    if (root == null)

    {

        this.SetError("Couldn't access node");

        return null;

    }

    return root;

}

 

AppPools are stored under:

 

IIS://localhost/W3SVC/AppPools

 

And you can access a specific pool through the Children collection. For ADSI paths a child looks like this:

 

IIS://localhost/W3SVC/AppPools/DefaultAppPool

 

From there you get a DirectoryEntry object and you can fire away on the properties of the pool and set thing like the impersonating account and various health checks. I didn’t need to look closely at this but I couldn’t find the MSDN documentation on IIsApplicationPool that shows the member properties. The MSDN documentation for IIS’s ADSI support (and even the IIS6 WMI support) is just plain awful and scattered through many places. If anybody happens to find a link with the member properties post it here.

 

Once you have an Application Pool it can be attached to a virtual directory/Application via its AppPoolId:

 

DirectoryEntry VDir = new DirectoryEntry("IIS://localhost/W3SVC/ROOT/WebStore");

VDir.Properties["AppPoolId"].Value = this.ApplicationPool;

VDir.CommitChanges();

 

And there you have it. I’ve updated the Web Configuration Utility online and it includes the AppPool configuration code, and a recent update to the West Wind Web Store also includes this code as part of the configuration.

Posted in IIS  

The Voices of Reason


 

Randy Minder
January 17, 2007

# re: Programmatically listing, getting and setting Application Pools

Rick - very useful post. Thank you. What code do you use to delete an app pool? Don't you have to make sure nothing is using the App pool first etc.?

Steve
January 17, 2007

# re: Programmatically listing, getting and setting Application Pools

This is a really useful post....thx.
Steve

Rick Strahl
January 17, 2007

# re: Programmatically listing, getting and setting Application Pools

Randy I haven't needed to delete an AppPool via code, but there's a Delete method you can invoke to remove the pool. I haven't tried this though, and I think you're probably right that you can't remove a pool that has attached virtuals. You can use EnumAppsInPool to get a list of the attached virtuals and I suppose you could manually assign another pool to each of those, then Delete.

But this is kind of a scary thing to do - I think I'd want somebody to manually do this and choose pools, so I would probably delegate THAT part to the Administration console.

Jamie Osborn
January 17, 2007

# re: Programmatically listing, getting and setting Application Pools

Sorry, this is O.T. but am I the only person using FeedDemon (or any other app) who gets all of Rick's posts twice?

It doesn't happen for any other blog but Rick's.

Rick Strahl
January 17, 2007

# re: Programmatically listing, getting and setting Application Pools

Hmmm... no. I use both FeedDemon and IE7's feed managers and both look fine. Actually the feed is not coming from my site, it's coming from Feedburner which is serving many community sites. I'd be really surprised if there's something wrong with the feed since they post-process what comes off my site.

Somebody else mentioned this before, but I don't see it. Anybody else see what Jamie is seeing?

Steve from Pleasant Hill
January 18, 2007

# re: Programmatically listing, getting and setting Application Pools

Really good!

Rick Strahl
March 23, 2007

# re: Programmatically listing, getting and setting Application Pools

Not efficiently no. You can use ADSI or WMI to read the Application Pool from the virtual directory but this is very slow - you'd want to do this once on application start and then write the value into a static value or Application State.

With ADSI you can use code to read the virtual directory, then read the ApplicationPool.

DirectoryEntry VDir = new DirectoryEntry("IIS://localhost/W3SVC/ROOT/WebLog");
string AppPoolId = VDir.Properties["AppPoolId"].Value;

# re: Programmatically listing, getting and setting Application Pools

I use this code snippet to get the application pool in which my website is running

 Public Shared Function getApplicationPoolName() As String
            Dim AppPath As String = Replace(Request.ServerVariables("APPL_MD_PATH"), "/LM/", "IIS://localhost/")
            Dim root As DirectoryEntry = New DirectoryEntry(AppPath)
            If (root Is Nothing) Then
                Return " no object got"
            End If
            Dim AppPoolId As String = root.Properties("AppPoolId").Value
            Return AppPoolId
        End Function

Bryan
May 01, 2007

# re: Programmatically listing, getting and setting Application Pools

i am getting Access Denied page

Microsoft VBScript runtime error '800a0046'

Permission denied: 'GetObject'

for DirectoryEntry root = this.GetDirectoryEntry("IIS://" + this.DomainName + "/W3SVC/AppPools");

this line why ?

Rick Strahl
May 01, 2007

# re: Programmatically listing, getting and setting Application Pools

You need to be an Admin to use DirectoryServices... a low privilege account won't work.

Manu
May 09, 2007

# re: Programmatically listing, getting and setting Application Pools

Dear friends,

which all namespaces are used other than System.DirectoryServices


thanks and regards
Manu

# DotNetSlackers: Programmatically listing, getting and setting Application Pools


george
June 04, 2007

# re: Programmatically listing, getting and setting Application Pools

Dear Rick, I love your articles. Please would you tell me how to attach an existing pool to Web site programmatically. Thanks a lot.

rony
June 28, 2007

# re: Programmatically listing, getting and setting Application Pools

thanks alot for this code


July 10, 2007

# 本周(月?)ASP.NET英文技术文章推荐[12/24 - 01/27] - Dflying Chen @ cnblogs - 博客园


have more now
April 25, 2008

# have more now

What a concept, just doing business right while at the same time working from home and having none of the headaches associated with having a JOB or owning a traditional business. Can you? Of course you can when you use the right strategy and take action. Of course you can;t when you continue doing the same thing as you have always done.

Russ
October 09, 2008

# re: Programmatically listing, getting and setting Application Pools

Hi,

I am trying to remove application pool isolation from all sites, but can;t seem to get it working, can you help, this is what I have so far. It sets the app pool back to default, but doesn't seem to disable the isolation, in the IIS MMC, the app pool should be greyed out, but it is not.
If I remove this manually via the MMC then I notice that all the app pool related entries in the metabase are actually removed rather than having theiur values changed, so I guess to a delete with my script, but how and what do I delete ?

'Turn off app pool isolation for all webs
Set objIIS = GetObject("IIS://" & getComputer() & "/W3SVC")
For Each objWeb in objIIS
If lCase(objWeb.Class) = "iiswebserver" Then
Set objVdir = GetObject("IIS://" & getComputer() & "/W3SVC/" & objWeb.Name & "/ROOT")
Wscript.Echo("Turn off app pool isolation: " & objWeb.ServerComment)
objVDir.AppPoolId = "DefaultAppPool"
objVdir.AppIsolated = 0
objVdir.setinfo()

End If
Next
Set objIIS = nothing

'Get computer name
Function getComputer()
Set objNet = WScript.CreateObject("WScript.Network")
getComputer= objNet.ComputerName
Set objNet = Nothing
End Function

Sammy
July 31, 2009

# re: Programmatically listing, getting and setting Application Pools

This is a very useful post. Also, to add an identity to the App Pool, here is the way to do it :

DirectoryEntry AppPool = root.Invoke("Create", "IIsApplicationPool", appPoolName) as DirectoryEntry;
AppPool.InvokeSet("AppPoolIdentityType", new Object[] { 3 });
AppPool.InvokeSet("WAMUserName", new Object[] { "username" });
AppPool.InvokeSet("WAMUserPass", new Object[] { "password" });
AppPool.Invoke("SetInfo", null);
AppPool.CommitChanges();

sushant
August 10, 2010

# re: Programmatically listing, getting and setting Application Pools

hay rick thanks... Its very helpfull......

Jeff
September 23, 2010

# re: Programmatically listing, getting and setting Application Pools

Any way to enable the App Pool running as a domain user access to reset its self with out giving the user Domain Admin privileges? We have an old ASP page that keeps getting memory errors until the App Pool is reset even thought the App Pool is set to recycle every 60 min, I have tried every 15 min but still get the errors. It would be nice to automate this but not put the site/domain at risk.

Ajay Narang
January 13, 2011

# re: Programmatically listing, getting and setting Application Pools

Maui, Hawaii

Excellent articles, thnx very much....I am also looking for code to attach application pool to VD.
Is AppPoolId returned in above code is same of application pool name

espinete
February 17, 2011

# re: Programmatically listing, getting and setting Application Pools

It's great article!!

it would be interesting how get virtual directories (that are website appliations) of a WebSite.

thx

hefang
June 28, 2011

# re: Programmatically listing, getting and setting Application Pools

i can not get the right AppPoolId, all AppPoolIds are 'DefaultAppPool'.
why?why?why?

West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2019