1 out of 1 rated this helpful - Rate this topic

Enumerating Applications in a Pool Using System.DirectoryServices

IIS 6.0

Calling methods of the unmanaged IIS Active Directory® Service Interfaces (ADSI) provider in managed code that use System.DirectoryServices are often complicated. The following code example uses the Invoke method of the DirectoryEntry class to call the IIS ADSI IIsApplicationPool.EnumAppsInPool (ADSI) method. The IIsApplicationPool.EnumAppsInPool (ADSI) method returns the list of applications in a parameter of the method, but because of the way the call is marshaled to the Invoke method, the list of applications is returned in an object array—not in the parameter list.

The following example shows you how to use the C# programming language to enumerate the friendly names of the applications that are contained in the specified application pool.

Note Note:

In IIS 5.1 and in earlier versions of IIS, application pools are not available.

In order to keep this code example concise, it does not include code access security (CAS) parameters or parameter checking. For more information on these topics, see Code Access Security and Validating User Input to Avoid Attacks. For additional security you can instantiate your System.DirectoryServices.DirectoryEntry object with an authentication parameter.


using System;
using System.IO;
using System.DirectoryServices;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Collections;

namespace System_DirectoryServices_DirectoryEntry_ConfigIIS
{
  class Program
  {
    static void Main(string[] args)
    {


...


EnumerateApplicationsInPool("IIS://localhost/W3SVC/AppPools/DefaultAppPool");


...


}


...


static void EnumerateApplicationsInPool(string metabasePath)
{
    //  metabasePath is of the form "IIS://<servername>/W3SVC/AppPools/<poolName>"
    //    for example "IIS://localhost/W3SVC/AppPools/DefaultAppPool" 
    Console.WriteLine("\nEnumerating applications for the {0} pool:", metabasePath);

    try
    {
        DirectoryEntry entry = new DirectoryEntry(metabasePath);

        if ("IIsApplicationPool" == entry.SchemaClassName)
        {
            object[] param;
            param = (object [])entry.Invoke("EnumAppsInPool", null);
            foreach (string s in param) Console.WriteLine("  {0}", s);
            Console.WriteLine(" Done.");
        }
        else
            Console.WriteLine(" Failed in EnumerateApplicationsInPool; {0} is not an app pool", metabasePath);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Failed in EnumerateApplicationsInPool with the following exception: \n{0}", ex);
    }
}


...


  }
}


Did you find this helpful?
(1500 characters remaining)