如何以编程方式回收.net Web应用程序自己的apppool? [英] How can I programmatically recycle a .net web app's own apppool?

查看:152
本文介绍了如何以编程方式回收.net Web应用程序自己的apppool?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Nhibernate和Linq2SQL的复杂服务器应用程序。每天大约3次Linq2sql代码生成值不能为空的异常。一旦发生这种情况,代码将始终生成异常。诊断和解决根本原因将是漫长的并且将引入不稳定性。

I have a complex server application that uses Nhibernate and Linq2SQL. About 3 times per day the Linq2sql code generates a "value cannot be null" exception. Once this happens, the code will always generate the exception. Diagnosis and solving the root cause will be lengthy and will introduce instability.

当前的修复是每小时重新调用应用程序池。但是,从问题发生到服务循环发生之前,服务已经停止。我希望Web服务捕获异常并回收它自己的应用程序池。我希望所有其他Web请求在完成之前得到尊重。

The current "fix" is to recyle the app pool every hour. However, the service is down from the point the problem happens until the recycle occurs. I want the web service to catch the exception and recycle it's own app pool. I want all other web requests to be honored until they are completed.

编辑:错误发生在负载均衡的Web场上的两台服务器上。客户端不会因为此代码崩溃而从一台服务器切换到另一台服务器。

The fault is on both servers on a load balanced web farm. Clients do not switch from one server to the other just because this code crashes.

推荐答案

以下代码将回收当前站点的应用程序池。您需要使用(ServerManager iisManager = new ServerManager())
添加对Microsoft.Web.Administration

The following code will recycle the current site's app pool. You need to add a reference to Microsoft.Web.Administration

using (ServerManager iisManager = new ServerManager())
{
    SiteCollection sites = iisManager.Sites;
    foreach (Site site in sites)
    {
       if (site.Name == HostingEnvironment.ApplicationHost.GetSiteName()) 
       {
         iisManager.ApplicationPools[site.Applications["/"].ApplicationPoolName].Recycle();
         break;
       }
    }
}

这篇关于如何以编程方式回收.net Web应用程序自己的apppool?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆