ASP.NET 4.0: How to use application warm-up class
About week ago I introduced IIS Application Warm-up Module. ASP.NET 4.0 provides separate mechanism for same purposes. ASP.NET 4.0 application warm-up is different and if you like you can use both warm-up methods together. In this posting I will show you how to make ASP.NET 4.0 application warm-up work.
At first you need ASP.NET 4.0 web application (Forms, MVC or Dynamic Data – you choose) that contains public class for warm-up. In this class we can do all work that needs to be done before application accepts requests. Let’s write simple class that cleans up cache folder.
Now we have class that cleans up cache folder and we can configure IIS now.
NB! Don’t forget to create cache folder on hard disc. Put some files in it and give IIS permissions to read and write to this folder. You need those files to check out if ASP.NET 4.0 application warm-up works.
To configure IIS you have to perform steps given below.
- Open %WINDOWSDIR%\system32\inetsrv\config\applicationHost.config.
- Locate <sites> block.
- Find your application definition and add the following attributes to <application> tag:
- Add the following XML after </site>:
type="MyWebApp.PreWarmUp, MyWebApp" />
- Save changes and close applicationHost.config (IIS should detect changes immediately).
Test your class
Now let’s see if our class works like expected. Open the folder with files you created in first step. Also open command prompt. Open it in administrative privileges and run iisreset. You should see files disappearing from your cache folder.
If it doesn’t happen then open Event Viewer and look for errors. If you have errors during warm-up then these errors are written to Event Log. If you compiled your application using Debug configuration you should get from Event Viewer all information about the error.
There are some limitations you should know about. You don’t have HTTP context available during warm-up. Your class must know where resources are located and how to access them. If you really need some warm-up work done under HTTP context then you can use IIS Application Warm-up Module. You can use both methods if you like.
I hope that in the future we have also method in IProcessHostPreloadClient interface that enables working under HTTP context. VS2010 RC is coming and I hope to find more warm-up options for ASP.NET 4.0 applications.
If you read documentation about IProcessHostPreloadClient interface you can find the following remark:
This interface is intended primarily for use by WCF applications that are non-HTTP applications. ASP.NET developers who want to preload ASP.NET Web applications should use the simulated HTTP requests in IIS 7.0 combined with the Application_Start method in the Global.asax file.
Keep it in mind when creating warm-up mechanism to your web application.
4 thoughts on “ASP.NET 4.0: How to use application warm-up class”
That is something we cant use in shared hosting scenario, too bad.
In shared hosting scenarios you should consider asking IIS Application Warm-up Module support from server admins.
What is the easiest way to fire Application_Start right after website deployment?
The easiest way to fire Application_Start is to use IIS Application Warm-up Module. Just read my post about it and you are done (http://weblogs.asp.net/gunnarpeipman/archive/2010/01/22/iis-application-warm-up-module.aspx)