Analysis of automatic starting characteristics of ASP. NET4

  • 2021-07-07 06:55:43
  • OfStack

The ability to initialize an web application without waiting for an external client to access the web server. This can help you give your first visitor a faster response experience, avoiding writing custom scripts to "warm up (warm up)" the server and preparing any data caches. It can be used for any type of ASP. NET applications, including applications based on ASP. NET, Web, Forms and ASP. NET, MVC.

Automatically start Web application in ASP. NET 4

Some web applications need to load a lot of data or do some costly initialization before they can handle user access. Developers who use ASP. NET today often use the "Application_Start" event handler in the application's Global. asax file to do this (this event is triggered when the first request is executed). They either design custom scripts that periodically send fake requests to the application to "wake it up (wake it up)" to execute the code before the customer visits, or let the unfortunate first visitor wait for the logic to execute before processing its request (which can cause long delays for these users).

A new feature in ASP. NET 4 called "Auto Start (auto-start)" addresses this scenario and can be used when running ASP. NET 4 on IIS 7.5 (released with Windows 7 and Windows Server 2008 R 2). This auto-start feature provides a controllable way to start an application worker process, initialize the ASP. NET application, and then accept the HTTP request.

Configure 1 ASP. NET 4 application to start automatically

To use the ASP. NET 4 auto-start feature, you first configure the IIS application pool worker process so that the applications running in it start automatically when the web server is first loaded. To configure this, open the applicationHost. config file for IIS 7.5 (C:\ Windows\ System32\ inetsrv\ config\ applicationHost. config) in the appropriate < applicationPools > Add 1 startMode = "AlwaysRunning" attribute to:


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

If you run the Windows Task Manager, click the "Show All Users' Processes" check box, and then change the startMode attribute of the applicationHost. config file and save it, you will see a new "w3wp. exe" worker process started immediately after the file is saved.

A single 1IIS application pool worker process can host multiple ASP. NET applications. You can use the < application > Add an serviceAutoStartEnabled= "true" attribute to specify which applications you want to start automatically when the worker process loads:


< 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 above refers to a provider (provider) configuration in the config file, allowing you to configure a custom class that encapsulates any "warm-up (warming up)" logic of the application. This class is automatically invoked when the worker process and application are preloaded (before any external web request is received) and can be used to perform any initialization or cache load logic that you want to perform before the request is accepted and processed:


public class PreWarmCache : System.Web.Hosting.IProcessHostPreloadClient { 
  public void Preload(string[] parameters) {  
    // Perform initialization and cache loading logic here...  
  }  
} 

IIS boots the application into a state where it cannot accept requests until your "warm-up (warming up)" logic is complete. After the initialization code in the Preload method runs and returns, the ASP. NET application is marked as processing the request.

You can also combine the new auto-start "warming up" feature with the extended load balancing function of IIS7 application request orientation (Application Request Routing, ARR) to signal the load balancer after the application is initialized and can accept HTTP requests. At this time, the server can put web farm to process the requests.

Concluding remarks

The new auto-launch features of ASP. NET 4 and IIS 7.5 provide a well-defined way to run expensive application startup and pre-cache logic before any end user accesses your application, which allows you to "warm up (warmed up)" the application from the beginning, providing a high-performance experience that is always as 1.

I hope this article will help you and have a deeper understanding of the automatic start features.


Related articles: