You may have noticed that the first request to an ASP.NET Web site takes longer than subsequent requests as worker threads are started, database connections opened and cached data loaded. One of my clients implemented a two-step process to eliminate this problem. First, they put code in a page on the site that would perform a bunch of initialization activities (primarily, load the Cache object with data). Second, they assigned some low-ranking person (often an intern) to request that page from the site. To ensure that the data is loaded before the first "real" user makes a request, the intern comes in very early in the morning to trigger the processing.

IIS 7.5 now has (in beta) an option to trigger code to run automatically whenever your Web site is started and before any requests are received -- just what my clients need. The good news is that this option works with any version of ASP.NET beginning with version 2.0. Because the feature is tied to IIS 7.5, you must be using Windows 7 or Windows Server 2008 R2 as your server.

If you want to start experimenting with the tool, first download the warmup module from http://www.iis.net/expand/applicationwarmup and install it on your server. There are two ways to configure this option: with IIS Manager or by making changes in configuration files.

Configuring with IIS Manager
In IIS Manager, drill down to the Web site you want to autostart and
double-click the Application Warm-up option in the panel in the middle
of IIS manager. This will display an empty list of URLs. In the right
hand "Actions" panel, click the Settings choice. That will display a
settings dialog where you must check both the "Enable Application
Warm-up" and the "Start Application Pool..." to enable autostart for the
application pool for your Web site.

Now click on Add Request in the right-hand Actions column. In the
resulting dialog, in the top textbox, enter the URL for a page on your
site that will execute the preloading code. All requests return a result
code so you need to indicate what result codes are acceptable in the
second textbox (in most cases, you can just leave the default setting of
200-399 in place).

You can also specify parameters to pass as part of the request and
whether the request is to made asynchronously (which can result in
multiple startup requests being made to the site at once) or
synchronously (where no request will be processed until the previous one
completes). For sites that won't accept requests without a username and
password (e.g. SharePoint sites), you can provide a user context.

Configuring with Config Files
You can also enable autostart with change in IIS 7.0's
applicationHost.config file (you should find the file in
C:\Windows\System32\inetsrv\config folder). To enable autostart for the
application pool, in the applicationPools element find the <add>
element for your site's application pool and add a startMode attribute,
setting it to "AlwaysRunning":

<applicationPools>
<add name="PHVISSite" startMode="AlwaysRunning" />

Now, in the applicationHost.config you must add a location element pointing to your site inside the file's configuration element. Doing this enables autostart for the site itself. Inside the location element's system.webserver element, use the httpWarmupGlobalSettings element to enable Warmup for the site:

<location path="PHVISSite">
<system.webServer>
<httpWarmupGlobalSettings httpWarmupEnabled="true" />
</system.webServer>
</location>

Now that you've enabled autostart for both your application pool and your Web site, you need to specify what page to request. You do that in the application's web.config file by adding an httpWarmup element containing a requests element. Within the requests element you use add elements to specify the page to request, the acceptable result codes, whether requests are to be processed synchronously or asynchronously, and a username/password combination. Here's an example:

<httpWarmup>
<requests>
<add requestUrl="PHVISSite/Default.aspx"
allowedResponseCodes="200-399" warmupContext=""
sendMode="Asynchronous" />
</requests>
</httpWarmup>

While I've been using the term "startup", the official term is "warmup," which better describes the process when the site's application pool is recycled rather than shut down and restarted. During a recycle, the old processing threads are kept running until the threads started by this warmup signal that they're done. Only then are the old threads wound down and new requests routed to the new threads -- you get a preloaded site with no interruption of service.

And, more importantly, your intern can sleep in! Actually, since you don't need him anymore, you can just let him go.

About the Author

Peter Vogel is a system architect and principal in PH&V Information Services. PH&V provides full-stack consulting from UX design through object modeling to database design. Peter tweets about his VSM columns with the hashtag #vogelarticles. His blog posts on user experience design can be found at http://blog.learningtree.com/tag/ui/.

Preloading Your ASP.NET Applications的更多相关文章

  1. [转]Publishing and Running ASP.NET Core Applications with IIS

    本文转自:https://weblog.west-wind.com/posts/2016/Jun/06/Publishing-and-Running-ASPNET-Core-Applications- ...

  2. ASP.NET中常用的优化性能的方法

    1. 数据库访问性能优化 数据库的连接和关闭 访问数据库资源需要创建连接.打开连接和关闭连接几个操作.这些过程需要多次与数据库交换信息以通过身份验证,比较耗费服务器资源.ASP.NET中提供了连接池( ...

  3. 保护ASP.NET 应用免受 CSRF 攻击

    CSRF是什么? CSRF(Cross-site request forgery),中文名称:跨站请求伪造,也被称为:one click attack/session riding,缩写为:CSRF/ ...

  4. VS2013中web项目中自动生成的ASP.NET Identity代码思考

    vs2013没有再分webform.mvc.api项目,使用vs2013创建一个web项目模板选MVC,身份验证选个人用户账户.项目会生成ASP.NET Identity的一些代码.这些代码主要在Ac ...

  5. 爱与恨的抉择:ASP.NET 5+EntityFramework 7

    EF7 的纠缠 ASP.NET 5 的无助 忘不了你的好 一开始列出的这个博文大纲,让我想到了很久之前的一篇博文:恋爱虽易,相处不易:当EntityFramework爱上AutoMapper,只不过这 ...

  6. ASP.NET 状态服务 及 session丢失问题解决方案总结

    ASP.NET2.0系统时,在程序中做删除或创建文件操作时,出现session丢失问题.采用了如下方法:1.asp.net Session的实现:asp.net的Session是基于HttpModul ...

  7. ASP.NET Misconfiguration: Missing Error Handling

    Abstract: An ASP .NET application must enable custom error pages in order to prevent attackers from ...

  8. ASP.NET Misconfiguration: Request Validation Disabled

    Abstract: Use the ASP.NET validation framework to prevent vulnerabilities that result from unchecked ...

  9. ASP.NET Misconfiguration: Debug Information

    Abstract: Debugging messages help attackers learn about the system and plan a form of attack. Explan ...

随机推荐

  1. jQuery-4.动画篇---动画基础隐藏和显示

    jQuery中隐藏元素的hide方法 让页面上的元素不可见,一般可以通过设置css的display为none属性.但是通过css直接修改是静态的布局,如果在代码执行的时候,一般是通过js控制元素的st ...

  2. winedt102安装

    http://www.xue51.com/soft/3171.html 安装是安装上了,还是用不了,提示系统找不到文件什么的.最后还是安装winedt7. 注意要配置,miktex,这个东西.wine ...

  3. 关于OSI

    OSI模型,即开放式通信系统互联参考模型(Open System Interconnection,OSI/RM,Open Systems Interconnection Reference Model ...

  4. 小妖精的完美游戏教室——东方PROJECT,同人,th12灵梦A

    ╮(╯▽╰)╭没办法,小妖精Balous也很讨厌学院化的教育呀,一点意义都没有. 这次就上传东方地灵殿灵梦A逻辑部分的核心代码吧,估计连老师都看不懂.动画部分的代码就不放上来了. //======== ...

  5. webpack打包和gulp打包工具详细教程

    30分钟手把手教你学webpack实战 阅读目录 一:什么是webpack? 他有什么优点? 二:如何安装和配置 三:理解webpack加载器 四:理解less-loader加载器的使用 五:理解ba ...

  6. 1130-Host '192.168.0.105' is not allowed to connect to this MySQL server的解决方案

    在CentOS 7服务器(192.168.0.118)上安装mysql5.7.17后,在本地(192.168.0.105)通过Navicat连接服务器上的MySQL报错,报错如图所示: Paste_I ...

  7. unity 常用插件 2

    Advanced PlayerPrefs Window 用来管理 PlayerPrefs 数据,超好用 JsonDotNet JSON 的序列化/反序列化插件.这个插件是支持 iOS, Android ...

  8. ssm项目导入activiti依赖后jsp页面el表达式报错

    错误原因:Tomcat8.x与activiti6.0依赖冲突导致 解决方法: 1.修改tomcat版本 2.在pom.xml中修改 在依赖中把 <dependency> <group ...

  9. EXCEL中,如何引用一个单元格中的数据,作为另一个单元格内容中的一部分?

    https://zhidao.baidu.com/question/230715654.html 假设单元格A1值是8(该值由函数计算得出),我要在单元格B1中引用A1的值,但只是作为B1单元格内容中 ...

  10. Flagr 配置说明

    说明文档来自官方文档 https://checkr.github.io/flagr/#/flagr_env 完整配置 包含了组件的配置参数以及说明,对于学习如何使用Flagr 还是很重要的,包含了数据 ...