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:
- 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
- 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:
ServerManager is expensive! make sure you use the “using” block in order to dispose it after you’re done with it.
1: static WorkerProcess currentWP;
2: static void Main(string args)
4: currentWP = new ServerManager().ApplicationPools.WorkerProcesses;
5: while (!Console.ReadLine().Equals("quit"))
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.
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.
How did you find that compared to the various PowerShell modules for managing it? In the gigs I have been to, it seemed Powershell was the preferred choice for these sorts of tasks.
Powershell is really good true, but no matter how good it is it is still best suitable for scripting purposes only; the minute you need IIS Administration in a situation that involves “maneuvering” behavior (more complicated logic decision, handling various exceptions, configuration management,…etc) then you’ll have change your tools accordingly, and that was the case here.