I've an application that remotely stops/restarts app pools/sites and checks the status of the app pools/sites as well. At present these are managed using the DirectoryEntry class e.g.
private DirectoryEntry FindSite(int nPort)
{
using (var sites = new DirectoryEntry(string.Format(m_adSitesPath,m_RemoteServerName)))
{
sites.RefreshCache();
foreach (DirectoryEntry de in sites.Children)
{
de.RefreshCache();
if (de.SchemaClassName == "IIsWebServer")
{
string port = GetNullableDirMultiValuePart(de, "ServerBindings", 0, 1);
if (nPort == int.Parse(port))
{
return de;
}
}
}
}
return null;
}
this method is failing on any servers running IIS 8, on another question it was suggested I should start looking at using the Microsoft.Web.Administrator classes. Could someone please point me in the right direction so I get a list of sites by Port using the Microsoft.Web.Administrator namespace?
The sample code is as below,
private List<Site> FindSite(int nPort)
{
var result = new List<Site>();
var server = new ServerManager();
foreach (Site site in server.Sites)
{
foreach (Binding binding in site.Bindings)
{
if (binding.EndPoint.Port == port)
{
result.Add(site);
break;
}
}
}
return result;
}
where MWA defines strong types (Site
, Binding
, and so on) instead of DirectoryEntry
which does not correspond to a meaningful type.
You can find the class reference here,
https://msdn.microsoft.com/en-us/library/microsoft.web.administration(v=vs.90).aspx