Click here to Skip to main content
Click here to Skip to main content

Programmatically Manage IIS

, 27 Nov 2008
Rate this:
Please Sign up or sign in to vote.
Base library for programmatically managing IIS through C# and Directory Services.

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 IIS
  • Schema 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sarafian
Team Leader ALGOSYSTEMS
Greece Greece
I live in Athens Greece and currently I am working with Business scale application with .NET latest technologies
 
I've been developing applications for personal and friends usage with C++ using majorly Borland's various IDEs since 1994.
In 2002 I began working for an R&D institute where I was introduced to C# which I worships ever since.
 
I love core application development and I would like to publish more articles here and on my blog, but there is not enough time to do so.
 
I usualy "waste" my spare time watching sitcoms, preferable SCI-FI.
I would like to play chess but I can't find any real world players to hang out with.

Comments and Discussions

 
QuestionUpgaded solution to VS2012 and got an error PinmemberMember 100617782-Aug-13 11:00 
AnswerRe: Upgaded solution to VS2012 and got an error PinmemberSarafian2-Aug-13 11:45 
QuestionConnect to Rremote Server Pinmembermanorithan5-Dec-11 21:48 
AnswerRe: Connect to Rremote Server PinmemberSarafian6-Dec-11 6:27 
GeneralI hope you can modify it !! PinmemberJLKEngine00825-Dec-08 22:38 
GeneralRe: I hope you can modify it !! PinmemberSarafian26-Dec-08 1:19 
This library was not intended to be a full application.
It was built basically to caver what I need for my work. On the way it proved to be a very good idea and its structure easy to extend with new functionality.
GeneralDelay and bug PinmemberSarafian3-Dec-08 1:55 
GeneralRe: Delay and bug PinmemberMarcel Wijnands19-Dec-08 0:35 
GeneralRe: Delay and bug PinmemberSarafian19-Dec-08 1:47 
GeneralThis looks interesting! Pinmemberhughd2-Dec-08 1:07 
GeneralRe: This looks interesting! PinmemberL Hills2-Dec-08 23:57 
GeneralRe: This looks interesting! PinmemberSarafian3-Dec-08 1:49 
GeneralRe: This looks interesting! PinmemberSarafian3-Dec-08 1:53 
GeneralExcellent Pinmembermerlin98128-Nov-08 4:08 
GeneralRe: Excellent PinmemberSarafian3-Dec-08 1:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140705.1 | Last Updated 27 Nov 2008
Article Copyright 2008 by Sarafian
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid