Recently, I was worked on a project that required me to automate the installation and creation of web sites on IIS6. I didn’t want to do this using VBScript or another WSH language because this was going to be used as part of a larger application. I made some exploratory attempts using the ADSI interfaces, but did not find them satisfactory due to performance concerns. After a little research, I discovered that since I was dealing with IIS6, that there were WMI interfaces available – so I decided to give that a whirl. What I discovered, was that the while the performance using WMI was not what I would call spectacular, it was definitely a large improvement over using the ADSI interfaces. The classes and code I’m sharing here are basic implementations, that accomplish only the basic tasks that I needed – so, there is definitely room for expansion on the part of the reader. This is only meant as a starting point – I am still actively extending and refactoring these implementations my self.
What I was creating was basically a generic deployment tool – basically, this tool takes a package (a zip file) that contains the web site content and directory structure and XML document that describes the details of the deployment based on the machine name in the infrastructure. This contains things like the name of directory to extract the package to, any web.config changes that need to be applied for the environment, and most importantly to this article a description of the IIS web site that should be created.
The first class I will share is the base class for most of the classes in my hierarchy – WmiObjectBase.
This class simply provides some common functionality for the various WMI based objects in my library.
The main class in the library, is the InternetInformationServer class. This class holds and manages the connection to an IIS6 instance. It allows you to create and manipulate the various server objects, such as web sites and application pools:
Please not the comments on the GetWebSite method… In the infrastructure I’m working in, I can guarentee that the server comment is unique for a given web site. In many cases, this can’t be guarenteed – so, really this method should be changed either to return a list of WebSite objects, or it should be refactored to take the site id – which is unique.
There are two overloads for the CreateWebSite method – one that takes a list of ServerBindings and one that doesn’t. You’ll notice that the overload that does not take a list of bindings simply delegates to the method that does, and passes in a default binding object.
Here are the ApplicationPool, WebSite, and ServerBinding objects as I currently have them:
Again these are relatively simple classes – but, again they have room to expand.
WebSite does have some properties and methods that maybe of interest. It allows you to start and stop the individual site – so you don’t have to bring down the entire IIS server, just to configure your site. It also allows you to get a reference to it’s root virtual directory so you can change settings – such as the framework version the site should use. Other properties of the root directory can be set via a dictionary of name value pairs. This was for me the most convenient method, since I was using xml files to store my configuration settings:
Here is some code that demonstrates how you might use the above library (Air-Code Warning!):
Anyway, I apologize if the level of detail here is less then expected – but I had to throw this together fairly quickly. I wanted to get it out there in the hopes that someone might find some of this useful.