Search code examples
c#iisiis-7configurationservermanager

Programmatically get site status from IIS, gets back COM error


I am trying to programmatically get my site status from IIS to see if it's stopped, but I kept getting the following error,

The object identifier does not represent a valid object. (Exception from HRESULT: 0x800710D8)

The application is using ServerManager Site class to access the site status. Here is the code,

//This is fine, gets back the site 
var serverManager = new Microsoft.Web.Administration.ServerManager(ConfigPath);
var site = serverManager.Sites.FirstOrDefault(x => x.Id == 5);
if (site == null) return;
var appPoolName = site.Applications["/"].ApplicationPoolName;
//error!
var state = site.State;

I've test with static site to isolate the issue, making sure that the site is up and running, all configuration are valid, point to the valid application pool...etc.

Let me know if you need more details. Is it the COM thing?


Solution

  • I figured out where the problem is. Basically, there are two parts to the Server manager, the first part of the server manager allows you to read site details from configuration file, which is what I've been doing above. The problem with that is you will only able get the information that's in file and site state is not part of it.

    The second part of the Server Manager allows you to connect to the IIS directly and it does this by interacting with the COM element. So what I should be doing is this:

    ServerManager manager= ServerManager.OpenRemote("testserver");
    var site = manager.Sites.First();
    var status = site.State.ToString() ;