Introduction
This article is about managing IIS programmatically. It provides a class library that represents the tree hierarchy used by IIS for its every aspect. The project is accompanied by a Windows Forms applications that is used to pinpoint and extract any data required.
Background
For the company I work, we have several applications of which two are N-Tier, requiring a Smart Client, an application server, and single or more databases. The deployment of our product to an existing client is difficult, and requires database schema upgrade and website restart. Additionally, creating a new environment needs to be parameterized. So, in order to create a Deploy and Environment Application, an IIS management know-how was required.
After searching online, I found some methods, but none that could give an object oriented approach to the problem. Also, there are few knowledge base articles, especially from Microsoft, about this stuff. For example, the version of IIS in a query could not easily be found, and whenever it was mentioned, it required a Registry or file query, which is not helpful when managing IIS remotely.
Explaining the library
The class library is DirectoryManager
. It was named like that because having dealt with Active Directory in the past, I came to realize that IIS is a single aspect of the same stuff with different data.
The understanding of the libray requires very good understanding of System.DirectoryServices
. The entry point for each node in the IIS hierarchy is the base class Base.BaseEntry
, which is a wrapper for a DirectoryEntry
and provides an additional functionality. The most important is a wrapper to the Properties
collection found in the directory entry. Some additional methods are provided like commit changes. In any case, the instance of the DirectoryEntry
and its children are exported via the SourceEntry
and Children
properties, respectively.
The properties are represented with the classes PropertyValue
and PropertyValues
. The primary reason is that the property value system of directory entry required a constant knowledge of the type of data and casting, making the code difficult to support and understand.
Sarafian.Framework.General.DirectoryManager.IIS
contains the classes that represent the IIS hierarchy, at least whatever was required for me. Every node of this hierarchy finds its corresponding sub-nodes, but also knows about its parent. Everything is typed, and if anything is missing, it can easily be added. In many cases, special care has been taken for version incompatibilities between versions 6 and 7 that have been tested. Every class node provides its member functions that add a subnode. For instance, the Service
class which represents the IIS service provides functionality to add and query websites. The WebSite
class provides functionality to start, stop, restart, and of course, check the status.
Here is what every class represents in the hierarchy.
Service:
It represents the IIS Service. It is the entry point for the IIS management system. The service holds the reference to single or multi AppPools
, Schema
, WebSite
and Info
classes.
AppPools
holds the list of AppPool
classes that logically describe the application pool feature of IISSchema
holds the AppIsIsolated
reference that, among others, knows whether the IIS service is under an NT system.Info
has the version info of the service. They are provided by MajorIISVersionNumber
and MinorIISVersionNumber
.WebSite
is the actual website. Each website on IIS has a SiteID
that really tells nothing about it, but is required to query and find the website. I can't understand why, but the actual physical path is held within a WebVirtualDir
which is almost identical to VirtualDirectory
. Here are some important methods. ConvertToApplication
makes a virtual directory an application for IIS and not just a simple folder, and ApplyASPNETV2
applies Asp.Net2
to its application pool.WebVirtualDir
is practically the root of the website.VirtualDirectory
represents a virtual directory on a website.
Both WebVirtualDir
and VirtualDirectory
have a PhysicalPath
property.
Some of the properties of child nodes are propagated to the owner node, because it seemed to me that they should be placed there. For example, WebSite
has a PhysicalPath
property, but it is actually a reference to PhysicalPath
in WebVirtualDir
.
Using the code
In the IISTreeView
, the service is created like this:
this.service = new Service("LocalHost");
The IIS hierarchy is represented with a tree, and when selecting on a node on the tree, a dataset with its properties is produced and shown on the right. Because all nodes of the IIS hierarchy have not been created in a typed class, a DirectoryEntryNode
exists that handles a simple DirectoryEntry
. Basically, this project could help you query anything that can be accessed through System.DirectoryServices
.
If a new property is required to be typed, then you create it with the same name and corresponding type. Extra functionality to parse, for example, data from the IIS must be implemented. For example, Port
on the WebSite
.
public int Port
{
get
{
try
{
return Convert.ToInt32(ServerBindings.Replace(":", ""));
}
catch (Exception)
{
}
return -1;
}
set { ServerBindings = String.Concat(":", value.ToString(), ":"); }
}
Points of interest
The state of the WebSite
has a problem, and requires a new DirectoryEntry
to be created for each call. So, this is used:
public WebSiteStates State
{
get
{
DirectoryEntry site = new DirectoryEntry(base.SourceEntry.Path);
return (WebSiteStates)Convert.ToInt32(site.Properties["ServerState"].Value);
}
}
Whenever a tree node requires to show all of its children in the tree, just add this in the constructor:
base.AddChildrenNodes();
The above structure may require more code, but it gives a typed and so much more meaningful representation of what happens in any Directory Service of Microsoft. That includes the Active Directory Service and, as far as I know, Microsoft Exchange. Microsoft is very secretive about the way things are stored in these services, so the Tree and Grid approach provides a helpful insight of the service which can be user to create the appropriate classes, and gradually build or extend a library.
History
This is version 1 of the library. It currently provides types functionality for IIS only.