简体   繁体   English

从应用程序的物理路径停止应用程序池-ServerManager C#

[英]Stop App Pools from Application's Physical Path - ServerManager C#

Using the ServerManager class is it possible to get the app pool name from the physical path for an application? 使用ServerManager类是否可以从应用程序的物理路径获取应用程序池名称? I have been testing with the following code: 我一直在测试以下代码:

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;
    }
}

} }

I would like to use the physical path to get the application rather than the virtual path as there are hundreds of applications in my applications DB and I can't be 100% that they have all been configured in IIS that the 2 paths would be in sync eg D:\\inetpub\\TestSite1 and /TestSite1 我想使用物理路径而不是虚拟路径来获取应用程序,因为我的应用程序数据库中有数百个应用程序,而且我不可能100%都已在IIS中配置了所有这两个路径。同步例如D:\\ inetpub \\ TestSite1和/ TestSite1

changing the fwd slashes to back slashes on this line of code seems to have did the trick for me: 在这行代码中将fwd斜杠更改为反斜杠似乎对我有用:

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

in my code I am now using a parameter instead of hardcoding the physical path so I have something like this: 在我的代码中,我现在使用参数,而不是对物理路径进行硬编码,因此我会遇到以下情况:

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)
      { ...

hope this helps someone else in the future. 希望这对以后的人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM