Miscellaneous Debris

Avner Kashtan's Frustrations and Exultations
Programmatically creating an IIS Application

Using System.DirectoryServices to manage IIS servers and directories is a long-established practice (or is it? Should I elaborate on that? Write a short article?), but I ran into a problem today when I wanted to turn an ordinary directory under IIS (6.0) into an Application. It seems that among the many Metabase properties available, the only one relevant is AppRoot, and the MSDN strongly urges one not to touch it since it is managed internally.

The alternative is simply to access the IisWebDirectory object (part of the IIS ADSI Object model) and call the AppCreate (or AppCreate2, or AppCreate3 under IIS6) to create the application.

Very nice and simple, except that I'm working with a DirectoryEntry object and don't have AppCreate to save my life.

The thing to do in this case is to access the NativeObject property of the DirectoryEntry, cast it into an IisWebDirectory, and call the method – but for some reason, I don't seem to have that class registered anywhere. The IIS Adsi object model (as added via References -> COM -> Active DS IIS Extension or Active DS IIS Namespace) don't seem to have IisWebDirectory or IisVirtualWebDir defined, which means I am quite stumped. Looked around for it, in the Registry and other common COM hangouts, but couldn't find any reference.

 

My final solution was to simply bypass the problem using direct means – reflection:

DirectoryEntry vdir = new DirectoryEntry ("IIS://localhost/W3SVC/1/Root/MyVDir");

Object nativeObject = vdir.NativeObject;

native.GetType().InvokeMember("AppCreate3", BindingFlags.InvokeMethod, null, native, new object[] {2, "DefaultAppPool", false}); // 2 means to use an Application Pool.

 

Is there a slightly neater solution? Does anyone know where the IisWebDirectory object is hiding?

Published Tuesday, July 27, 2004 9:47 PM by AvnerK

Filed under:

Comments

# re: Programmatically creating an IIS Application@ Tuesday, July 27, 2004 3:13 PM

I was kinda wondering this myself, and the only stuff I could find was this: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/iissdk/iis/creating_application_pools_using_system_directoryservices.asp

Looks pretty similiar to what you are doing

Phil Scott

# re: Programmatically creating an IIS Application@ Thursday, April 26, 2007 12:38 AM

Am I going insane, or is that MSDN currently showing no code samples?

David Keaveny

Leave a Comment

(required) 
(required) 
(optional)
(required)