IIS 解决asp.net 网站第一次访问过慢的解决方案

一、调整应用程序池

  (1)将应用程序池高级属性中的启动模式更改为”AlwaysRunning”

  (2)将应用程序池高级属性中的回收固定间隔时间更改为0,取消固定间隔时间回收

  (3)将应用程序池高级属性中的特定时间回收,设置为访问量较少的时间

  (4)将应用程序池高级属性中的闲置超时更改为0

二、在.net项目中创建一个类,用于项目初始话后自动请求网站  

namespace CMS.Web
{
    public class ApplicationPreload : System.Web.Hosting.IProcessHostPreloadClient
    {
        public void Preload(string[] parameters)
        {
            try
            {
                //自动请求的url,其中http://localhost:8001 最好配置在config中,这边只是演示。
                string url = Common.ConfigHelper.GetConfigString("host");
                using (var webClient = new WebClient())
                {
                    webClient.DownloadStringCompleted += WebClient_DownloadStringCompleted;
                    webClient.Encoding = Encoding.UTF8;
                    webClient.DownloadStringAsync(new Uri(url));//要异步请求   
                    
                }
            }
            catch (Exception e)
            {
                LogHelper.logHelper.ErrorLog(e.Message, e.StackTrace);
            } 
        } 

        private void WebClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
           
                LogHelper.logHelper.InfoLog(DateTime.Now.ToString(),e.Result) ; 
        }
    }
}

第三步、修改iis配置文件

  (1)添加serviceAutoStartProviders:name可随意定义,type为”第二步创建的类的全类名,命名空间“,全类名与命名空间用逗号隔开

  (2)修改sites:选中要修改的站点,将preloadEnabled 和serviceAutoStartEnabled修改为true,将上一步创建的name添加到serviceAutoStartProvider中。保存即可

原文地址:https://www.cnblogs.com/mrma/p/13066675.html