如何解决IIS的应用程序池回收问题

方法一:IIS预加载

1,修改应用程序池的启动模式为:AlwaysRunning (高级设置中)

2,开启对应网站预加载(IIS7.5没有这个)选中站点(高级设置中-预加载已启动:True)

3,增加配置编辑器,编写默认预加载的请求页面 hostName是地址,initializationPage是要加载的页面

方法二:通过代码实现iis回收之后自动访问页面来唤醒服务

Global.asax 中
protected void Application_End()
        {
            WriteLog("进程即将被IIS回收"+DateTime.Now);
            WriteLog("重新访问一个页面,以唤醒服务" + DateTime.Now);
            string strURL = System.Configuration.ConfigurationManager.AppSettings["homeURL"].ToString();
            try
            {
                System.Net.WebClient wc = new System.Net.WebClient();
                System.IO.Stream stream = wc.OpenRead(strURL);
                System.IO.StreamReader reader = new StreamReader(stream);
                string html = reader.ReadToEnd();
                if (!string.IsNullOrWhiteSpace(html))
                {
                    WriteLog("唤醒程序成功" + DateTime.Now);
                }
                reader.Close();
                reader.Dispose();
                stream.Close();
                stream.Dispose();
                wc.Dispose();
            }
            catch (Exception ex)
            {
                WriteLog("唤醒异常"+ex.Message+DateTime.Now);
            }
        }
经验分享 程序员 微信小程序 职场和发展