网上新建程序池的方法很多,唯独删除程序池方法比较少,所以我记录下删除程序池的方法。
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:空间域名、雅安服务器托管、营销软件、网站建设、贡井网站维护、网站推广。网上找到这种
/// <summary> /// 删除指定程序池 -虚拟机上跑不通 /// </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; }
试了下本地能跑通,虚拟机上跑不通,报的错毫无用处,不知道怎么办的情况下问了下我领导,我领导提示我用ServerManage的山删除方法,于是有了
/// <summary> /// 根据程序池名称删除程序池 - 好用 /// </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"); }
期间,走了很多弯路,现在解释下。
因为我要实现的方法是删除服务器上所有不在使用的程序池,所以开始思路是
由于没有找到方法来获取Application下面应用程序,就不好判断程序池在不在使用中,所以用以下方法来判断程序池是不是在使用中:
获取ServerManager的Site,获取Site的Application,获取Application的ApplicationPoolName,存入使用中程序池列表中,
获取ServerManager的ApplicationPools,用foreanch遍历每个Pool,如该Pool不在使用中程序池列表中,就调用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(); }
遍历时第一次时候正常,第二个时就会报错,报错如下:
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
分析后发现foreach中Applications第二次用的时候由于上一次有remove而不能用了。考虑用以下方法:
每次传入一个需要删除的applicationname,
然后像事务一样,需新建一个servermanager,用该servermanager得到application,再用该manager删除这个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> /// 根据程序池名称删除程序池 /// </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
如此就能正常工作了。
创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。
本文标题:删除程序池-创新互联
转载来于:https://www.cdcxhl.com/article28/ddesjp.html
成都网站建设公司_创新互联,为您提供面包屑导航、建站公司、云服务器、网站导航、App开发、ChatGPT
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联