使用 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 中配置为 2 条路径将同步,例如 D:\inetpub\TestSite1 和/TestSite1
将
这行代码上的 fwd 斜杠更改为反斜杠似乎为我提供了
诀窍:var application = site.Applications.SingleOrDefault(m => m.VirtualDirectories["/"].PhysicalPath == @"D:inetpubTestSite1");
在我的代码中,我现在使用参数而不是硬编码物理路径,所以我有这样的东西:
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)
{ ...
希望这将来对其他人有所帮助。