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.

Warm-up class

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.

Configuring IIS

To configure IIS you have to perform steps given below.

  1. Open %WINDOWSDIR%\system32\inetsrv\config\applicationHost.config.
  2. Locate <sites> block.
  3. Find your application definition and add the following attributes to <application> tag:
    serviceAutoStartEnabled="true" serviceAutoStartProvider="PrewarmMyApp"
  4. Add the following XML after </site>:
            type="MyWebApp.PreWarmUp, MyWebApp" />
  5. 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.

Current limitations

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.

Liked this post? Empower your friends by sharing it!
Categories: ASP.NET

View Comments (4)

Related Post