簡體   English   中英

檢查IIS應用程序池的狀態

[英]check the status of IIS Application Pool

我正在開發Windows服務,每隔5分鍾就會獲取IIS應用程序池狀態信息,並將其存儲在數據庫或文本文件中,例如運行或停止。

獲取以下異常消息:

Microsoft.Web.Administration.dll中發生類型'System.UnauthorizedAccessException'的異常,但未在用戶代碼中處理。其他信息:訪問被拒絕。 (來自HRESULT的異常:0x80070005(E_ACCESSDENIED))

以下是我嘗試過的代碼:

 static void Main(string[] args)
    {
        const double interval60Minutes = 5 * 5 * 1000; // milliseconds to one hour
        Timer checkForTime = new Timer(interval60Minutes);
        checkForTime.Elapsed += new ElapsedEventHandler(checkForTime_Elapsed);
        checkForTime.Enabled = true;
        Console.WriteLine("Waiting..");           
        Console.ReadLine();          
    }

    public static void checkForTime_Elapsed(object sender, ElapsedEventArgs e)
    {            
        GetApplicationPoolNames();
    }       

    public static string GetApplicationPoolNames()
    {
        ServerManager manager = new ServerManager();
        string status;
        //string DefaultSiteName = System.Web.Hosting.HostingEnvironment.ApplicationHost.GetSiteName();
        //Site defaultSite = manager.Sites[DefaultSiteName];
        string appVirtaulPath = HttpRuntime.AppDomainAppVirtualPath;
        string mname = System.Environment.MachineName;
        string appPoolName = string.Empty;
        manager = ServerManager.OpenRemote(mname);
        ObjectState result = ObjectState.Unknown;

        ApplicationPoolCollection applicationPoolCollection = manager.ApplicationPools;

        foreach (ApplicationPool applicationPool in applicationPoolCollection)
        {
            //result = manager.ApplicationPools[appPoolName].State;
            result = applicationPool.State;  *// here exception occures*
            Console.WriteLine("State : " + result);
            Console.ReadLine();
        }
   }

代碼有什么問題? 如果還有其他方法可以實現此目的,請提供,因為這也將幫助我理解異常消息的主要原因。

任何幫助表示贊賞。

謝謝。

提出的問題太舊了,但是想共享解決方案,當嘗試獲取/讀取服務器的IIS應用程序池信息時,該解決方案可能會幫助某人解決此類錯誤/異常。

要在嘗試訪問IIS信息時解決未經授權的訪問異常,請首先使用DirectoryEntry類提供服務器憑據,如下所示-

DirectoryEntries appPools = new DirectoryEntry("IIS://" + ServerName + "/W3SVC/AppPools", UName, Pwd).Children;

這樣可以訪問相應服務器的IIS。

因此,修改后的完整GetApplicationPoolNames()方法是-

public static string GetApplicationPoolNames()
{
    // Get Server Credentials and Server Name from config file
    string UName = ConfigurationManager.AppSettings["User"];
    string Pwd = ConfigurationManager.AppSettings["Pass"];
    string ServerName = DT.Rows[i]["ServerName"].ToString().Trim(); //Server Names from db
    DirectoryEntries appPools = null;
    try
    {
        appPools = new DirectoryEntry("IIS://" + ServerName + "/W3SVC/AppPools", UName, Pwd).Children;
    }
    catch(Exception ex)
    {
        log.ErrorFormat("serviceLogic -> InsertStatus() -> IIS Pool App Region -> DirectoryEntries -> Error: ", ex.Message.ToString());
    }

    log.Info("IIS App Pool Section Started for " + System.Environment.MachineName.ToString());

    try
    {
        foreach (DirectoryEntry appPool in appPools)
        {
            log.Info("App Pool : " + appPool.Name.ToString());
            int intStatus = 0;
            string status = "";
            try
            {
                if (appPool.Name.ToString().ToLower().Trim() == DT.Rows[i]["AppPoolSrvName"].ToString().ToLower().Trim())
                {
                    log.Info("Process Started for App Pool : " + appPool.Name.ToString());

                    intStatus = (int)appPool.InvokeGet("AppPoolState");
                    switch (intStatus)
                    {
                        case 2:
                            status = "Running";
                            break;
                        case 4:
                            status = "Stopped";
                            break;
                        default:
                            status = "Unknown";
                            break;
                    }

                    //Store status info to db or file Logic goes here..

                    //Start App pool, If any application pool status is not Running.
                    if (status != "Running")
                        appPool.Invoke("Start", null);

                    log.Info("Process Completed for App Pool : " + appPool.Name.ToString());
                }
            }
            catch (Exception ex)
            {
                log.ErrorFormat("serviceLogic -> InsertStatus() -> IIS Pool App Region -> Error: ", ex.Message);
            }
        }
    }
    catch (Exception ex)
    {
        log.ErrorFormat("serviceLogic -> InsertStatus() -> IIS Pool App Region -> DirectoryEntries -> Error: ", ex.Message);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM