Back to Home

Autostart ASP.NET applications

asp.net 4 · auto-start

Autostart ASP.NET applications

Original author: Scott Gu
  • Transfer
imageThis article shows a new small but pleasant feature that optionally allows you to automate the launch and proactive initialization of a web application, without having to wait for an external visit by the client to the server. This will help provide a quick answer for the first user who visited the server and will avoid writing your own code that will “warm up” the server and place the necessary information in caches. This innovation works in all types of ASP.NET applications, including Web Forms and MVC.

Autostart web applications in ASP.NET 4

Some web applications require a large amount of data to be loaded or a time-consuming initialization process to process requests. Developers using ASP.NET today often use the “Application_Start” event in the Global.asax file for this, the first time they are executed when requests are launched. They invent their own scripts to send false requests to the application, to periodically “wake up” and run this code before the user or expect the unfortunate first user to wait until certain actions work out before processing the first request.

ASP.NET 4 provides a new feature called “auto-start” that better implements this scenario and is available on IIS 7.5 (which installs on Windows 7 and Windows Server 2008 R2). Autostart allows you to controlfully join the launch of the application worker-process, initialize the ASP.NET application, and then accept HTTP requests.

Configuring ASP.NET 4 applications for autorun

To use autorun, you need to configure the IIS “application pool” worker process, which is automatically started by the application when the web server first loads. You need to open applicationHost.config in IIS 7.5 (C: \ Windows \ System32 \ inetsrv \ config \ applicationHost.config) and add the startMode = ”AlwaysRunning” attribute to the node:


  


If you start the Windows task manager and select “Show processes of all users”, and then save the edited applicationHost.config file, you will see a new process “w3wp.exe”, which starts immediately after saving the file.

A single IIS application worker process pool can host multiple ASP.NET applications. You can choose which application starts automatically when the process loads by adding the serviceAutoStartEnabled = “true” attribute in the configuration:



  

     

  






  



The serviceAutoProvider = “PreWarmMyCache” attribute allows you to configure your own class, which will use any logic to “warm up” the application. This class will be automatically called as soon as the worker process and application are preloaded (before receiving any external web requests) and can be used to start any initialization or caching logic.
public class PreWarmCache : System.Web.Hosting.IProcessHostPreloadClient {

  public void Preload(string[] parameters) {

    // Perform initialization and cache loading logic here...

  }

}

IIS will launch the application in a state during which requests will not be accepted before your “warm-up” logic fails. After your initialization code starts and runs in the Preload method, the ASP.NET application will be marked as ready to process requests.

You can combine the new autostart capability with the load balancing extension for IIS7 - Application Request Routing (ARR), signaling the balancer as soon as the applications are initialized and ready to receive HTTP traffic, therefore, the server can be placed in a web farm to process requests.

Read Next