Most of us have experienced that lesser visited DNN (or other ASP.Net) web sites need some time to start when browsing to the first page. The reason for this is the shut-down of the ASP.Net worker process that happens after no request to the Application Pool has been done for a given number of time - the default is 20 minutes. Beyond that, the worker process is shut down every 29 hours (1740 minutes) by default even when it is not idle.
What happens when a request comes in?
These are the first steps that happen against a client request:
- IIS checks whether a worker process is active or not.
- If not:
- IIS it creates the worker process by starting w3wp.exe.
- The worker process hosts aspnet_isapi.dll - this is the handler mapped to "aspx" files. IIS checks for the script map and routes the request to the handler.
- The request is passed to the .Net runtime (which is hosted in the worker process as well).
- The application domain is created by a class called ApplicationDomain. This provides isolation between different applications, e.g. for global variables, and allows to unload applications separately.
- Inside the application domain, the hosting environment is created as an instance of the HostingEnvironment class. It provides information about the application such as a unique application ID, the physical path on disk where it is stored, the name of the site etc.
- ASP.Net creates core objects like HttpContext, HttpRequest, HttpResponse etc.
- The application is started as an instance of the HttpApplication class - or, if there is a global.asax file, it creates an instance of it's own class (that derives from HttpApplication).
- If required, ASP.Net compiles the top-level items in the application (including code in the App_Code folder).
Keep Alive Services
It is (or should be) quite clear that this takes some time, and therefore it is quite clear why we have to wait sometimes. The good news is that you can do something
against it. One solution could be to increase the value(s) for the shut-down(s) in IIS Manager by configuring the application pool. This will help only if you can
say that you have one visitor e.g. once an hour and set the time-out to e.g. 90 minutes - but are you really sure about this?
The other way is to use a keep-alive service, such as the great and free DNN Monitor that makes a request
to your web site every five minutes - and also records response times and provides some statistics for you.
But what if this is a website in your intranet that is not accessible for DNN Monitor?
There are two things to know:
DNN provides a script named KeepAlive.aspx (which resides in the root of the DNN installation). This script is doing nearly nothing, it contains
a call to an ASP.Net method that returns the server's current date and time, and uses the HTML-Meta-Tag to reload itself every 5 minutes.
- Windows contains a very powerful command line shell named Windows Power Shell.
Knowing the first one allows you to install a keep-alive service. Just start a browser on any PC in your network and call the KeepAlive.aspx page (e.g. by
typing http://intranet.company.local/KeepAlive.aspx in your browser's address bar. Don't close the browser, don't log off or shut down the
computer and you're done.
There is a more elegant way. You can write a Windows Power Shell script using your preferred text editor. The script requests the web site,
and then you can call the script on a regular basis using a scheduled task. The script is not really complicated, it just instantiates a web client object
and requests the page:
$WebClient = New-Object Net.WebClient
Save the script somewhere on your web server, e.g. C:\PowerShellScripts\KeepAlive.ps1. Open Power Shell, navigate to the folder where you stored
it and execute the script:
PS H:\> C:
PS C:\> CD \PowerShellScripts
PS C:\PowerShellScripts> .\KeepAlive.ps1
(... Lot of stuff ...)
Got an error? The reason
is most propably the execution policy that requires signed scripts. You can change this to Remote Signed by entering
You can avoid the output by assigning the last call to a variable, so change the second line of the code to
$Source = $WebClient.DownloadString("http://intranet.company.local/")
The last step is to create a scheduled task on your web server that starts powershell.exe under the SYSTEM account every 5 or 10 minutes with the name of the
script as parameter. You can do this using the graphical interface for Scheduled Tasks in Windows or using the SCHTASKS command (in a command window or
SCHTASKS /CREATE /RU SYSTEM /RL HIGHEST /TN "Keep Web Site Alive" /SC minute /MO 10 /TR "powershell.exe C:\PowerShellScripts\KeepAlive.ps1"