📜 ⬆️ ⬇️

Autorun ASP.NET applications

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

Autorun web applications in ASP.NET 4

Some web applications require downloading large amounts of data or perform a time-consuming initialization process, before being able to process requests. Developers using ASP.NET today often use the “Application_Start” event in the Global.asax file, which is executed for the first time when running queries. They invent their own scripts to send false requests to the application, to periodically "wake up" and run this code before the user or suggest the unfortunate first user to wait for certain actions to work before processing the first request.

ASP.NET 4 provides a new feature called “auto-start”, which better implements this script and is available on IIS 7.5 (which is installed on Windows 7 and Windows Server 2008 R2). Autorun allows you to control the launch of an application's worker-process, initialize an ASP.NET application, and then accept HTTP requests.
')
Configuring ASP.NET 4 Autorun Applications

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

< add name ="MyAppWorkerProcess" managedRuntimeVersion ="v4.0" startMode ="AlwaysRunning" />

</ applicationPools >

If you start the Windows Task Manager and select “Display processes for all users”, and then save the edited applicationHost.config file, you will see the new process “w3wp.exe”, which starts immediately after saving the file.

One IIS pool of an application's worker-process can host several ASP.NET applications. You can choose which application automatically starts when the process is loaded by adding the serviceAutoStartEnabled = "true" attribute in the configuration:

< sites >

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

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

</ site >

</ sites >

< serviceAutoStartProviders >

< add name ="PreWarmMyCache" type ="PreWarmCache, MyAssembly" />

</ serviceAutoStartProviders >


The serviceAutoProvider = “PreWarmMyCache” attribute allows you to configure your own class, which will be used by any application “warming up” logic. This class will be automatically called as soon as the worker-process and the application are preloaded (before receiving any external web requests) and can be used to run 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, until your “warm-up” logic is completed. After your initialization code starts and runs in the Preload method, the ASP.NET application will be marked as ready for processing requests.

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

Source: https://habr.com/ru/post/73272/


All Articles