網(wǎng)上新建程序池的方法很多,唯獨(dú)刪除程序池方法比較少,所以我記錄下刪除程序池的方法。
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長期合作伙伴,公司提供的服務(wù)項(xiàng)目有:空間域名、雅安服務(wù)器托管、營銷軟件、網(wǎng)站建設(shè)、貢井網(wǎng)站維護(hù)、網(wǎng)站推廣。網(wǎng)上找到這種
/// <summary> /// 刪除指定程序池 -虛擬機(jī)上跑不通 /// </summary> /// <param name="appPoolName">程序池名稱</param> /// <returns>true刪除成功 false刪除失敗</returns> public static bool DeleteAppPool(string appPoolName) { Logger.Info("In Method DeleteAppPool()"); bool result = false; DirectoryEntry appPools = new DirectoryEntry("IIS://localhost/W3SVC/AppPools"); foreach (DirectoryEntry entry in appPools.Children) { Logger.Info("In Entry :" + entry.Name); if (entry.Name.Equals(appPoolName)) { try { Logger.Info("delete Entry :" + entry.Name); entry.DeleteTree(); Logger.Info("delete success"); result = true; break; } catch { result = false; } } } return result; }
試了下本地能跑通,虛擬機(jī)上跑不通,報(bào)的錯(cuò)毫無用處,不知道怎么辦的情況下問了下我領(lǐng)導(dǎo),我領(lǐng)導(dǎo)提示我用ServerManage的山刪除方法,于是有了
/// <summary> /// 根據(jù)程序池名稱刪除程序池 - 好用 /// </summary> /// <param name="poolName"></param> public static void DeleteAppPool(string poolName) { Logger.Info("in DeleteAppPool()"); ServerManager manager = new ServerManager(); manager.ApplicationPools.Remove(manager.ApplicationPools[poolName]); Logger.Info("FInish Remove:" + poolName); manager.CommitChanges(); Logger.Info("finsh commit"); }
期間,走了很多彎路,現(xiàn)在解釋下。
因?yàn)槲乙獙?shí)現(xiàn)的方法是刪除服務(wù)器上所有不在使用的程序池,所以開始思路是
由于沒有找到方法來獲取Application下面應(yīng)用程序,就不好判斷程序池在不在使用中,所以用以下方法來判斷程序池是不是在使用中:
獲取ServerManager的Site,獲取Site的Application,獲取Application的ApplicationPoolName,存入使用中程序池列表中,
獲取ServerManager的ApplicationPools,用foreanch遍歷每個(gè)Pool,如該P(yáng)ool不在使用中程序池列表中,就調(diào)用ServerManager.ApplicationPools.Remove方法刪除
代碼如下:
public static List<string> GetAppPoolNameInUse() { ServerManager manager = new ServerManager(); ApplicationPoolCollection pools = manager.ApplicationPools; SiteCollection sites = manager.Sites; List<string> poolnameinuse = new List<string>(); foreach (Site site in sites) { ApplicationCollection applications = site.Applications; foreach (Application application in applications) { string name = application.ApplicationPoolName; if (!poolnameinuse.Contains(name)) { poolnameinuse.Add(name); } } } return poolnameinuse; } public static void ClearAppPoolNotInUse() { List<string> poolnameinuse = GetAppPoolNameInUse(); List<string> poolnamenotinuse = new List<string>(); ServerManager manager = new ServerManager(); ApplicationPoolCollection apppools = manager.ApplicationPools; foreach (ApplicationPool pool in apppools) { if (!poolnameinuse.Contains(pool.Name)) { manager.ApplicationPools.Remove(pool); } } manager.CommitChanges(); }
遍歷時(shí)第一次時(shí)候正常,第二個(gè)時(shí)就會(huì)報(bào)錯(cuò),報(bào)錯(cuò)如下:
Exception:2015/8/27 13:06:49異常信息:System.InvalidOperationException:Collecti
on was modified;enumeration operation may not execute.
at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resour
ce)
at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
at System.Collections.Generic.List`1.Enumerator.MoveNext()
at MonitorWarning.IISUtil.ClearAppPoolNotInUse() in d:\Ctrip_Hotel\study_demo
\MonitorWarning\MonitorWarning\IISUtil.cs:line 1010
at MonitorWarning.Program.Main(String[] args) in d:\Ctrip_Hotel\study_demo\Mo
nitorWarning\MonitorWarning\Program.cs:line 24(System.InvalidOperationException:
Collection was modified; enumeration operation may not execute.
at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resour
ce)
at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
at System.Collections.Generic.List`1.Enumerator.MoveNext()
at MonitorWarning.IISUtil.ClearAppPoolNotInUse() in d:\Ctrip_Hotel\study_demo
\MonitorWarning\MonitorWarning\IISUtil.cs:line 1010
at MonitorWarning.Program.Main(String[] args) in d:\Ctrip_Hotel\study_demo\Mo
nitorWarning\MonitorWarning\Program.cs:line 24)
Finished
分析后發(fā)現(xiàn)foreach中Applications第二次用的時(shí)候由于上一次有remove而不能用了。考慮用以下方法:
每次傳入一個(gè)需要?jiǎng)h除的applicationname,
然后像事務(wù)一樣,需新建一個(gè)servermanager,用該servermanager得到application,再用該manager刪除這個(gè)application
代碼如下:
#region 清除不在使用中的程序池 public static List<string> GetAppPoolNameInUse() { ServerManager manager = new ServerManager(); ApplicationPoolCollection pools = manager.ApplicationPools; SiteCollection sites = manager.Sites; List<string> poolnameinuse = new List<string>(); foreach (Site site in sites) { ApplicationCollection applications = site.Applications; foreach (Application application in applications) { string name = application.ApplicationPoolName; if (!poolnameinuse.Contains(name)) { poolnameinuse.Add(name); } } } return poolnameinuse; } public static void ClearAppPoolNotInUse() { List<string> poolnameinuse = GetAppPoolNameInUse(); List<string> poolnamenotinuse = new List<string>(); ServerManager manager = new ServerManager(); ApplicationPoolCollection apppools = manager.ApplicationPools; foreach (ApplicationPool pool in apppools) { if (!poolnameinuse.Contains(pool.Name)) { poolnamenotinuse.Add(pool.Name); } } foreach (string name in poolnamenotinuse) { DeleteAppPool(name); } } /// <summary> /// 根據(jù)程序池名稱刪除程序池 /// </summary> /// <param name="poolName"></param> public static void DeleteAppPool(string poolName) { Logger.Info("in DeleteAppPool()"); ServerManager manager = new ServerManager(); manager.ApplicationPools.Remove(manager.ApplicationPools[poolName]); Logger.Info("FInish Remove:" + poolName); manager.CommitChanges(); Logger.Info("finsh commit"); } #endregion
如此就能正常工作了。
創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國云服務(wù)器,動(dòng)態(tài)BGP最優(yōu)骨干路由自動(dòng)選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機(jī)房獨(dú)有T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進(jìn)行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動(dòng)現(xiàn)已開啟,新人活動(dòng)云服務(wù)器買多久送多久。
本文標(biāo)題:刪除程序池-創(chuàng)新互聯(lián)
轉(zhuǎn)載來于:http://jinyejixie.com/article28/ddesjp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、建站公司、云服務(wù)器、網(wǎng)站導(dǎo)航、App開發(fā)、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容