Moving to the .net paradigm a decade ago, the JIT compiler has always been a nasty part of most of us developing the ASP.Net applications. I cannot recollect how many times I have wished that I was able to provide exactly the same experience to the first user that the subsequent users would see. So this includes, pre-loading of assemblies, pre-executing mechanisms that built data cache and a countless number of possibilities. A lot of times, we used to achieve these using some hacks to either write a separate process that would trigger some dummy requests just to boot up the application or to write some ISAPI handlers or HTTP modules to do it if it is an internet facing site.
Well, as always is with the hard days – they need to end somewhere. I recently discovered this ASP.net 4.0 feature that allows us to do just exactly that – auto-start the applications. Quite a no-brainer that the feature is titled "auto-start".
The ASP.net auto start feature allows us to automatically boot up the ASP.net applications and execute certain code that would be critical for the efficient functioning of the app - for instance, loading items into an in-memory cache or just pre-loading certain heavy assemblies.
The feature is partly enabled by the functionality in IIS 7.5 that helps load the worker process and application proactively. Hence IIS 7.5 is definitely required for this to work.
Getting it to work
What changes on the configuration?
To use this feature, you will need to modify the applicationHost.config file under (C:\Windows\System32\inetsrv\config\applicationHost.config) and change the start mode of the application pool to "AlwaysRunning". This ensures that the application pool is always running and does not stop after a couple of minutes of inactivity – which is the case for the default setting of the application pools.
Now since one app pool usually hosts more than one web application, it is also important that the web application specifies explicitly if it wants to perform any pre-load tasks. This is done very easily by adding a "ServiceAutoStartEnabled" attribute on the specific web site in the same file and setting it to "true".
As the last bit in the configuration, we also need to create an entry for autostart provider and reference it at the site level. So, when completed, sections of your web config would look something similar to –
path="/" serviceAutoStartEnabled="true" serviceAutoStartProvider=" MySitePreLoad" />
name="MySitePreLoad" type="MyTypeForWarmup, SomeAssembly" />
In some of the cases, you might encounter a file not found error when trying to edit this file in Visual Studio. The best way that I found to circumvent this is to simply open the file in Notepad – it always works.
What changes in the code?
As you might've noticed in the above snippets, we need to specify a type for the auto start service providers. As with most of the types that you specify in the configs, this one also needs to be written in a particular way. The below code snippet demonstrates how we can specify the pre-load logic in code.
void Preload(string parameters)
// Code for pre load goes here.
This class and the code in the PreLoad method is what will get executed on the appPool start and recycle events. Once this method executes completely, the ASP.net application will be marked as "Ready for processing requests" and subsequently IIS will start sending requests your way.
This is definitely one of the features that I have been longing for on the ASP.net sites and I am pretty sure it will help you in a lot of scenarios as well.