Wednesday, July 11, 2007

IIS6: separated application pool

When IIS runs multiple versions of .NET web projects, collision happens sometimes.
So, I required to assign my web project in separated application pool under Windows 2003 /IIS6 programmatically.

It is easy through System.DirectoryServices of .NET
There is, I create virtual directory "myWebDir" and assign it to new pool with the name "myAppPool":


DirectoryEntry folderRoot = new DirectoryEntry("IIS://localhost/W3SVC/1/Root");
DirectoryEntry virDir = folderRoot.Children.Add("myWebDir", "IIsWebVirtualDir");
object[] param ={ 0, "myAppPool", true };
virtDir.Invoke("AppCreate3", param);
//set virtual directory properties
//.......
newVirDir.CommitChanges();




Application pool is created automatically if third parameter is a true in the param array.

However, I would like also to manage my application pools without specific virtual directory.
Here are examples to create/remove application pools:

Create Application pool:


string appPoolName = "myAppPool";
DirectoryEntry poolRoot = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
if (!DirectoryEntry.Exists(IISAppPoolRootPath + "/" + appPoolName);
{
DirectoryEntry pool = poolRoot.Children.Add(appPoolName, "IIsApplicationPool");
pool.CommitChanges();
}





Remove Application pool:


string appPoolName = "myAppPool";
DirectoryEntry poolRoot = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
DirectoryEntry pool = poolRoot.Children.Find(appPoolName, "IIsApplicationPool");
if (pool != null)
{
poolRoot.Children.Remove(pool);
poolRoot.CommitChanges();
}




Remove only works, if no one web application assigned to pool.

No comments: