Tuesday, April 29, 2014

From IIS 7.5 onwards IIS has the ability to use prewarming functions to get the stuffs done before anyURL hit to ther server. Say you want to fill your cache, so that you can avoid lot of processing time. But before IIS 7.5 this was done in the Application_Start of Global.asax. The disadvantage in doing this is, this code will be executed on the first URL hit. But thats not what we want. To speed up this what developers used to do is, they make fake request to the server inorder to invoke the prewarming method. To overcome all these things IIS 7.5 came up with a functionality called AutoStart.
The AutoStart option basically needs to be enabled for our site and we need to provided serviceAutoStartProviders which will provide the pre warming functionality. I will explain it step by step here.
1. Open IIS manager --> Features View --> Configuration Editor and open the section "system.applicationHost/applicationPools"
2. Change the AutoStart option as displayed in the picture below and click Apply.



3. Now in the same Configuration Editor open the section "system.applicationHost/serviceAutoStartProviders"
4. Add a new serviceAutoStartProvider. In the name field provide a name for your serviceAutoStartProviders and in type provide the type of the prewarmer assembly in the following format
(Namespace.ClassName, AssemblyName)  eg: (MyNamespace.MyClass, prewarmer) if your assembly is prewarmer.dll 
(Note: There's a catch here. You need to provide the namespace name along with class name.)
5. Most of the time if you follow these steps you’ll see a new “w3wp.exe” worker process immediately startup as soon as you save the configuration. But if you don't see it starting, nothing to worry. Just open the app pool config file for your app pool in the following location.  "C:\inetpub\temp\appPools\YourWebsite\YourAppPool.config" and make changes as below.


     

     <site name="MySite" id="1">

          <application path="/" serviceAutoStartEnabled="true" serviceAutoStartProvider="PreWarmMyCache" />

     </site>



Once you save the config file after theese changes you will see the new “w3wp.exe” worker process immediately starting up.

Okay now what's the deal with assembly. Nothing much guys, Just implement the IProcessHostPreloadClient as below and add your loading code in the preload fucntion. No need to worry about the string[] parameters.

public class MyClass : System.Web.Hosting.IProcessHostPreloadClient 
{
    public void Preload(string[] parameters) 
{
//Prewarming logic here
    }
}

Thats all folks. Your website is now ready to prewarm. Restart your AppPool and all set to go. Event log will provide you if your prewarm code contains any unhandled exception causing app pool to stop.

Happy Coding,
Dheena

No comments:

Post a Comment