0

使用 ServerManager 类是否可以从应用程序的物理路径中获取应用程序池名称?我一直在使用以下代码进行测试:

using (var manager = new ServerManager())
//using (var manager = ServerManager.OpenRemote("xxx")) //Uncomment this line to enable remote debugging, comment out line 1821, update Server Name value
{
    //Get Site using Website ID
    var site = manager.Sites.SingleOrDefault(m => m.Id == webSiteID);

    if (site != null)
    {
        //foreach (Application app in site.Applications)
        //{
            bld.AppendLine("1. Stopping App Pool for: " + site.Name);
            //Get the application pool name
            var application = site.Applications.SingleOrDefault(m => m.VirtualDirectories["/"].PhysicalPath == @"D:/inetpub/TestSite1"); //RETURNS NULL
            //Get the application binded to the siteName
            var application1 = site.Applications.SingleOrDefault(m => m.Path == "/TestSite1"); //RETURN INFO I REQUIRE

            var appPoolName = application.ApplicationPoolName;
            //var appPoolName = site.Applications.FirstOrDefault().ApplicationPoolName;
            var appPool = manager.ApplicationPools[appPoolName];

            if (appPool != null)
            {
                //Check the App Pool State
                if (appPool.State == ObjectState.Stopped)
                {
                    //App Pool already stopped
                    bld.AppendLine("1. App Pool: " + appPool.Name + " already stopped.");
                }
                else
                {
                    //Stop the App Pool
                    appPool.Stop();
                    bld.AppendLine("1. App Pool: " + appPool.Name + " stopped.");
                }
            }
            else
            {
                bld.AppendLine("1. No App Pool found.");
                failFlag = true;
            }                            
        //}                        
    }
    else
    {
        bld.AppendLine("1. SiteID: " + webSiteID + " does not exist.");
        failFlag = true;
    }
}

}

我想使用物理路径而不是虚拟路径来获取应用程序,因为我的应用程序数据库中有数百个应用程序,而且我不能 100% 认为它们都已在 IIS 中配置,两条路径都在同步例如 D:\inetpub\TestSite1 和 /TestSite1

4

1 回答 1

0

在这行代码中将 fwd 斜杠更改为反斜杠似乎对我有用:

var application = site.Applications.SingleOrDefault(m => m.VirtualDirectories["/"].PhysicalPath == @"D:\inetpub\TestSite1");

在我的代码中,我现在使用参数而不是硬编码物理路径,所以我有这样的东西:

using (var manager = new ServerManager())                
{
   //Get Site using Website ID
   var site = manager.Sites.SingleOrDefault(m => m.Id == webSiteID);

   if (site != null)
   {
      bld.AppendLine("1. Stopping App Pool for: " + site.Name + " " + targetPath);
      var application = site.Applications.SingleOrDefault(m => m.VirtualDirectories["/"].PhysicalPath == @targetPath);
      var appPoolName = application.ApplicationPoolName;
      var appPool = manager.ApplicationPools[appPoolName];
      if (appPool != null)
      { ...

希望这对将来的其他人有所帮助。

于 2016-02-08T16:02:43.323 回答