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

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

问题描述

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

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.

故障出在负载平衡网络场的两台服务器上.客户端不会因为这段代码崩溃而从一台服务器切换到另一台服务器.

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.

推荐答案

下面的代码会回收当前站点的应用池.您需要添加对 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.SiteName) 
       {
         iisManager.ApplicationPools[site.Applications["/"].ApplicationPoolName].Recycle();
         break;
       }
    }
}

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

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