Search code examples
c#iisconfigurationiis-7.5servermanager

How does ServerManager finds the states of an given site


I am trying to read the status of a site using Servermanager. Basically this is what I have,

var serverManager = new ServerManager(siteInstance.Server.ConfigPath);
    var site = serverManager.Sites.FirstOrDefault(x => x.Id == Convert.ToInt64(siteInstance.IisIdentifier));
    return site.State.ToString();

I am able to read the config file and site details without any issue. But the status of the site is either giving me COM error below or giving an status that doesn't reflect the actual status of the site in IIS.

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

To my understanding, config file only contains the site information. It doesn't indicate which IIS servers are reading from the config. So how does the ServerManager know which IIS to look into to look for the running status of the site?


Solution

  • The reason you are getting the site is because you are trying to read the status from site configuration file, which doesn't contain the state of the site. Instead, what you should be doing is connect directly to IIS server like this:

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

    Please refer to my post below for full details: Programmatically get site status from IIS, gets back COM error