程序实现回收应用程序池--管理

 

最近网站偶尔出现无法访问数据库,研究了很长时间才发现是应用程序池问题,回收应用程序池后错误恢复正常。在程序加了一段操作IIS的代码,当出现访问数据库错误时就跳转到导常处理页面回收应用程序池,之后返回用户访问页面,这样用户就不会发现报告错误,给用户一个好的体验。

 

示例A:

  1.          private void ManAppPool
  2.         {
  3.                 
  4.         //应用程序池
  5.         ConfigAppPool("Recycle","Expo_China4auto");
  6.         Response.Redirect(Request.UrlReferrer.ToString());
  7.                         
  8.          }
  9.     //method是管理应用程序池的方法,有三种Start、Stop、Recycle,AppPoolName是应用程序池名称 
  10.     private void ConfigAppPool(string method,string AppPoolName) 
  11.     { 
  12.         DirectoryEntry appPool = new DirectoryEntry("IIS://localhost/W3SVC/AppPools"); 
  13.         DirectoryEntry findPool = appPool.Children.Find(AppPoolName,"IIsApplicationPool"); 
  14.         findPool.Invoke(method,null); 
  15.         appPool.CommitChanges(); 
  16.         appPool.Close(); 
  17.     }

 

 示例2: 遍历 IIS 6应用程序池的一个方法

  1. System.DirectoryServices.DirectoryEntry appPoolRoot
  2.     = new System.DirectoryServices.DirectoryEntry(@"IIS://localhost/W3SVC/AppPools");
  3. //得到默认应用程序池的方法可以直接使用 IIS://localhost/W3SVC/AppPools/DefaultAppPool
  4. System.Collections.IEnumerator AppPoolEnumer = appPoolRoot.Children.GetEnumerator();
  5. while (AppPoolEnumer.MoveNext())
  6. {
  7.     System.DirectoryServices.DirectoryEntry EntryPool 
  8.         = (System.DirectoryServices.DirectoryEntry) AppPoolEnumer.Current;
  9.     System.DirectoryServices.PropertyCollection properties = EntryPool.Properties;
  10.     System.Collections.IDictionaryEnumerator propertiesEnumer = properties.GetEnumerator();
  11.     textBox1.Text += "应用程序池名称 = " + EntryPool.Name + System.Environment.NewLine 
  12.         + "____________________________________________" + System.Environment.NewLine;
  13.     while (propertiesEnumer.MoveNext())
  14.     {
  15.         System.DirectoryServices.PropertyValueCollection propertyvalue 
  16.             = (System.DirectoryServices.PropertyValueCollection) propertiesEnumer.Value;
  17.         if (propertyvalue.Count > 1)
  18.         {
  19.             for (int j = 0; j < propertyvalue.Count; j++)
  20.             {
  21.                 textBox1.Text += "Name=" + propertiesEnumer.Key.ToString() 
  22.                               + "  Value= " + propertyvalue[j] + "--";
  23.             }
  24.         }
  25.         else
  26.         {
  27.             textBox1.Text += "Name=" + propertiesEnumer.Key.ToString() 
  28.                             + "  Value= " + propertyvalue[0] + System.Environment.NewLine;
  29.         }
  30.     }
  31. }

来源:http://groups.google.com/group/microsoft.public.cn.dotnet.framework.aspnet/msg/bdd2c95baee7a637?hl=en&

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值