2

我创建了一个应用程序,它在 IIS 中获取我的所有站点并检查绑定中的每个 URL,如果存在 HTTP 错误并且遇到某个错误,我的应用程序将重置 IIS 中的 IIS 站点实例,但是,这还不够按照应有的方式修复错误。我需要重置它所属的站点和应用程序池。

有什么方法可以根据 Site 对象获取 Application Pool 对象吗?

我已经尝试了下面的代码,但这仅适用于应用程序池中只有 1 个站点/应用程序的情况。问题是,如果站点列表与应用程序池列表之间的数字不匹配,我将无法获得站点的匹配应用程序池,因为 1 个应用程序池可以有多个站点/应用程序。

ServerManager serverMgr = new ServerManager();
SiteCollection sites;
ApplicationPoolCollection appPools;
public List<(Site, ApplicationPool, string)> getSiteInfo()
{
    List<(Site, ApplicationPool, string)> siteInfo = new List<(Site, ApplicationPool, string)>();
    List<string> siteUrls = new List<string>();
    sites = serverMgr.Sites;
    appPools = serverMgr.ApplicationPools;
    foreach (Site site in sites)
    {
        foreach (ApplicationPool appPool in appPools)
        {
            foreach (Binding binding in site.Bindings)//getting site url
            {
                string bindingInfo = binding.BindingInformation; // "192.111.1.1:80:google.com" /// *:808:
                string[] adrs = bindingInfo.Split(':'); //0 = ip, 1 = port, 2 = hostname
                if (adrs[0] == "*")
                {
                    adrs[0] = "localhost";
                }
                //adding to my list of sites and it's corresponding Application Pool in 1 tuple variable
                siteInfo.Add((site, appPool, adrs[0] + ":" + adrs[1] + "/" + adrs[2])); //localhost:80/google.com 
            }
        }
    }
    return siteInfo;
}

我需要类似于此代码的内容:(请参阅评论)

public List<(Site, ApplicationPool, string)> getSiteInfo()
{
    List<(Site, ApplicationPool, string)> siteInfo = new List<(Site, ApplicationPool, string)>();
    List<string> siteUrls = new List<string>();
    sites = serverMgr.Sites;
    foreach (Site site in sites)
    {
        foreach (Binding binding in site.Bindings)
        {
            //I need something like this to make sure the AppPool I'm getting is of the Site I have.
            ApplicationPool appPool = site.ApplicationPoolName;//<-- This Line
            string bindingInfo = binding.BindingInformation;
            string[] adrs = bindingInfo.Split(':');
            if (adrs[0] == "*")
            {
                adrs[0] = "localhost";
            }
            //So that I can do this when passing the Site Info tuple Variable with the Site Object together with the corresponding AppPool for later use of the IISReset Class in my project.
            siteInfo.Add((site, appPool, adrs[0] + ":" + adrs[1] + "/" + adrs[2]));
        }
    }
    return siteInfo;
}

抱歉,解释冗长而草率,但如果您对此有疑问,我很乐意澄清。谢谢你。

4

1 回答 1

0

我通过使用站点的应用程序属性来解决这个问题。为了获取站点的应用程序池,我执行了以下代码。我在哪里获得了站点的应用程序列表,并使用它从服务器中的应用程序池列表中识别应用程序池。但是,这仅在站点只有 1 个应用程序时才有效,这就是为什么我要索引 0(零)以获取第一个应用程序(这是我的站点的唯一应用程序)来搜索其相应的应用程序池。

ApplicationCollection apps = site.Applications;
ApplicationPool appPool = serverMgr.ApplicationPools[appname[0].ApplicationPoolName];
于 2019-11-13T06:49:24.217 回答