Monthly Archives: September 2012

Microsoft.Web.Administration for IIS

Update: In addition to this post, you can check as well the valuable post of Stuart Cullinans’s.

One of the recent projects I worked on involved managing IIS programmatically, and I found the proper tool for it, meet “Microsoft.Web.Administration”.

You can read about this library’s purpose in its own page above; what I will list here are three points I noticed while dealing with the library:

  1. I couldn’t find where to download, or install the library from, it appeared to be residing in “C:\Windows\SysWOW64\inetsrv\Microsoft.Web.Administration.dll” and yes my OS is 64-bit I am not sure where you can find that on a 32-bit machine
  2. The main class ServerManager reads data the moment it’s created only, it will NOT maintain a valid state after the initial read; for example, consider the following snippet:
  3.    1: static WorkerProcess currentWP;

       2: static void Main(string[] args)

       3: {

       4:     currentWP = new ServerManager().ApplicationPools[0].WorkerProcesses[0];

       5:     while (!Console.ReadLine().Equals("quit"))

       6:     {

       7:         Console.WriteLine(currentWP.ProcessId.ToString());

       8:     }

       9: }

    This code snippet will get the first WorkerProcess of the first Application Pool in the local IIS. Run the code, enter bogus input just to make sure the loop works, and you will find on the Console the W3WP Process Id displayed (e.g. 4295).

    Now open the Task Manager and kill that process, run the loop again you will find that the process ID is still being displayed, even though the process itself is gone!

    The way you think this would act is to throw an exception when you access a property of the object after it’s invalidated, this doesn’t happen!

    So you have to be careful not to hold for this library’s objects for long; for accurate reading, create them just before you need them.

  4. ServerManager is expensive! make sure you use the “using” block in order to dispose it after you’re done with it.

I believe these three notes could be in any library that have access to resources on the machine, so you better keep that in mind for any other library of the like.