最近网站偶尔出现无法访问数据库,研究了很长时间才发现是应用程序池问题,回收应用程序池后错误恢复正常。在程序加了一段操作IIS的代码,当出现访问数据库错误时就跳转到导常处理页面回收应用程序池,之后返回用户访问页面,这样用户就不会发现报告错误,给用户一个好的体验。
示例A:
- private void ManAppPool
- {
- //应用程序池
- ConfigAppPool("Recycle","Expo_China4auto");
- Response.Redirect(Request.UrlReferrer.ToString());
- }
- //method是管理应用程序池的方法,有三种Start、Stop、Recycle,AppPoolName是应用程序池名称
- private void ConfigAppPool(string method,string AppPoolName)
- {
- DirectoryEntry appPool = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
- DirectoryEntry findPool = appPool.Children.Find(AppPoolName,"IIsApplicationPool");
- findPool.Invoke(method,null);
- appPool.CommitChanges();
- appPool.Close();
- }
示例2: 遍历 IIS 6应用程序池的一个方法
- System.DirectoryServices.DirectoryEntry appPoolRoot
- = new System.DirectoryServices.DirectoryEntry(@"IIS://localhost/W3SVC/AppPools");
- //得到默认应用程序池的方法可以直接使用 IIS://localhost/W3SVC/AppPools/DefaultAppPool
- System.Collections.IEnumerator AppPoolEnumer = appPoolRoot.Children.GetEnumerator();
- while (AppPoolEnumer.MoveNext())
- {
- System.DirectoryServices.DirectoryEntry EntryPool
- = (System.DirectoryServices.DirectoryEntry) AppPoolEnumer.Current;
- System.DirectoryServices.PropertyCollection properties = EntryPool.Properties;
- System.Collections.IDictionaryEnumerator propertiesEnumer = properties.GetEnumerator();
- textBox1.Text += "应用程序池名称 = " + EntryPool.Name + System.Environment.NewLine
- + "____________________________________________" + System.Environment.NewLine;
- while (propertiesEnumer.MoveNext())
- {
- System.DirectoryServices.PropertyValueCollection propertyvalue
- = (System.DirectoryServices.PropertyValueCollection) propertiesEnumer.Value;
- if (propertyvalue.Count > 1)
- {
- for (int j = 0; j < propertyvalue.Count; j++)
- {
- textBox1.Text += "Name=" + propertiesEnumer.Key.ToString()
- + " Value= " + propertyvalue[j] + "--";
- }
- }
- else
- {
- textBox1.Text += "Name=" + propertiesEnumer.Key.ToString()
- + " Value= " + propertyvalue[0] + System.Environment.NewLine;
- }
- }
- }
来源:http://groups.google.com/group/microsoft.public.cn.dotnet.framework.aspnet/msg/bdd2c95baee7a637?hl=en&