Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

 
 
 
DZone MVB

Links

Social

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.

C#

using System.Web.Hosting;

using System.IO;

 

namespace MyWebApp

{

    public class PreWarmUp : IProcessHostPreloadClient

    {

        public void Preload(string[] parameters)

        {

            var cacheFolder = "e:\\cache";

            string[] files = Directory.GetFiles(cacheFolder);

 

            foreach (var file in files)

            {

                File.Delete(file);

            }

        }

    }

}


VB.NET

Imports System.Web.Hosting

Imports System.IO

 

Namespace MyWebApp

    Public Class PreWarmUp

        Implements IProcessHostPreloadClient

        Public Sub Preload(ByVal parameters As String()) _

            Implements IProcessHostPreloadClient.Preload

 

            Dim cacheFolder = "e:\cache"

            Dim files As String() = _
                Directory.GetFiles(cacheFolder)

 

            For Each file__1 In files

                File.Delete(file__1)

            Next

        End Sub

    End Class

End Namespace


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>:

  5. <serviceAutoStartProviders>

        <add
            name="PrewarmMyApp"
           
    type="MyWebApp.PreWarmUp, MyWebApp" />

    </serviceAutoStartProviders>


  6. 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.

kick it on DotNetKicks.com pimp it 顶 Progg it Shout it
Shout it!
Posted: Jan 31 2010, 03:23 PM by DigiMortal | with 14 comment(s)
Filed under: ,

Comments

Zubair.NET! said:

Hi,

That is something we cant use in shared hosting scenario, too bad.

# January 31, 2010 8:41 AM

DigiMortal said:

In shared hosting scenarios you should consider asking IIS Application Warm-up Module support from server admins.

# January 31, 2010 8:43 AM

Koistya `Navin said:

What is the easiest way to fire Application_Start right after website deployment?

# January 31, 2010 1:00 PM

DigiMortal said:

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 (weblogs.asp.net/.../iis-application-warm-up-module.aspx)

# January 31, 2010 2:44 PM