Home > Uncategorized > Start an IIS website synchronously with ADSI

Start an IIS website synchronously with ADSI

Using ADSI, once you have a DirectoryEntry object for a given website using

var root = new DirectoryEntry(“IIS://localhost/W3SVC”);
foreach (DirectoryEntry e in root.Children)
{
if (e.SchemaClassName != “IIsWebServer”) continue;

// e now represents a website in IIS

}

You can start and stop it using

Entry.Invoke(“start”);

Entry.Invoke(“stop”);

However, these commands run asynchronously, and you need to poll on the value

Entry.Invoke(“Get”, “ServerState”)

Where 4 is stopped and 2 is started. However, those using IISAdmin.net will find that this value never changes, meaning that it is impossible to tell when the website has actually changed state. Therefore, my fix was calling:

Entry.RefreshCache();

Prior to calling Invoke/Get

Advertisement
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: