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?
1 Comment
Comments have been disabled for this content.
David Keaveny said
Am I going insane, or is that MSDN currently showing no code samples?