Posts tagged IISConfig

Using ahadmin to read/write IIS configuration – Part 1

The Application Host Administration API (ahadmin.idl) interface library can be accessed using native code and any COM-interop means like script or managed code. This blog post details how to use this interface in scripts (all samples are in javascript) to read/write IIS7 configuration information.

Getting/Setting property values

You start by getting an instance of IAppHostAdminManager if you want to only read the config. If you want to write to config as well, you will need an instance of IAppHostWritableAdminManager which derives from IAppHostAdminManager. IAppHostWritableAdminManager has additionally CommitChanges method to commit changes to disk and CommitPath get/set property which dictates where the configuration settings are written. Lets create an instance of these.

Tool to generate strongly typed classes for configuration sections

I wrote a simple tool to generate strongly typed classes for IIS configuration sections which can then be used with MWA to enable intellisense. This tool should be able to generate very logical intuitive type names in most cases. Generated code is similar to code samples in my earlier blog.

Usage:
genscode.exe <schemaFile> <optional section name>

This tool dumps the code on the console. Redirect to save in a file.
genscode.exe %windir%\system32\inetsrv\config\schema\IIS_Schema.xml system.webServer/httpCompression > HttpCompression.cs

MWA and intellisense for configuration sections

Microsoft.Web.Administration (MWA) returns generic ConfigurationSection, ConfigurationElementCollection, ConfigurationElement classes for dealing with different configuration sections. Using these classes directly requires you to remember attribute/collection/element names which needs to be passed to GetAttribute(), GetCollection(), GetChildElement(). MWA allows you to define your Section/Collection/Element types which can then be passed to GetSection, GetCollection, GetChildElement to get an instance of strongly typed class which makes dealing with sections very easy.