網站由於使用比較多的緩衝,有時會出現緩衝錯誤,導致程式異常,這個時候需要回收一個應用程式集區就可以了。
以前每次都是通過遠端桌面連線到伺服器,然後在應用程式集區上右擊》回收,感覺比較麻煩,於時找了如下的一個辦法來實現。
其實就是普通的一個asp.net頁面,加上一個按鈕,進行回收,主要程式如下: 複製 儲存
protected void StartStopRecycleApp(string method){string AppPoolName = this.tbAppName.Text.Trim();//string method = "Recycle";try{DirectoryEntry appPool = new DirectoryEntry("IIS://localhost/W3SVC/AppPools"); DirectoryEntry findPool = appPool.Children.Find(AppPoolName, "IIsApplicationPool");findPool.Invoke(method, null);appPool.CommitChanges();appPool.Close();lbMsg.Text = string.Format("應用程式集區{0}{1}成功", AppPoolName, method);}catch (Exception ex){lbMsg.Text = string.Format("應用程式集區{0}{2}失敗:{1}", AppPoolName, ex.Message, method);}}
tbAppName是一個textbox,用來輸入應用程式集區的名字,如“DefaultAppPool”。
當method="Recycle"時就是回收,為“Start”時是啟動,為“Stop”時是停止。
注意:
1. 必須引入System.DirectoryServices包
2. 運行此程式的應用程式也的使用者必須許可權比較高,可以單獨為此程式提供應用程式程,或者建立一個虛擬目錄在配製裡類比進階使用者(如administrators或者system),否則應用程式會拋出“拒絕訪問”的異常。