Use the Automatic startup feature of ASP. NET 4

Source: Internet
Author: User

The Visual Studio 2010 and. NET 4.0 tutorial series of Scott Gu, head of Microsoft. NET platform, continue. We are now in article 7. Today, Scott Gu will talk about the new runtime features. For the previous articles, refer:

Article 1 Article 2 Article 3 Article 4 Article 5 Article 6

I want to change the topic of the discussion, from discussing the features of the new VS 2010 tool, to writing a few posts to discuss some new runtime special gender concerns, I will also turn back to discuss more VS features. I just want to mix them ).

Today's post discusses an ASP. NET 4 is small, but a great new feature, that is, the ability to automatically start and initialize web applications first, without waiting for external clients to access the web server. This helps you provide a faster response experience for the first visitor, avoiding writing a custom script to "Push warm up" the server and preparing any data cache. 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 applications in ASP. NET 4

Some web applications need to load a lot of data or perform initialization before they can process user access. Today, ASP. NET developers often use the "Application_Start" event handler in the Global. asax file of the application to perform these tasks. This event is triggered when the first request is executed ). They either design custom scripts and periodically send fake requests to the application to "wake it up)" to execute the code before the customer accesses it, or let the unfortunate first visitor wait until the logic is executed to process the request, which can cause a long delay for these users ).

ASP. NET 4 has a new feature called "auto-start automatically started)", which can better solve this scenario and can be released in IIS 7.5 with Windows 7 and Windows Server 2008 R2) run ASP.. NET 4. This Automatic startup feature provides a controllable way to start an application workflow, initialize an ASP. NET application, and then accept HTTP requests.

Configure an ASP. NET 4 Application to automatically start

To use the Automatic startup feature of ASP. NET 4, you must first configure the IIS application pool worker process so that the applications running in it are automatically started when the web server is first loaded. The configuration method is to open the applicationHost of IIS 7.5. config File (C: \ Windows \ System32 \ inetsrv \ config \ applicationHost. in <applicationPools>, add the startMode = "AlwaysRunning" attribute:

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

Worker.

A single IIS application pool worker can host multiple ASP.. NET application. You can add the serviceAutoStartEnabled = "true" attribute to the <application> of the corresponding application to specify which applications you want to automatically start when the worker process is loaded:

 
 
  1. < sites>   
  2.      < site name="MySite" id="1">   
  3.           < application path="/" serviceAutoStartEnabled="true" serviceAutoStartProvider="PreWarmMyCache" /> 
  4.      < /site>   
  5. < /sites> 
  6. < serviceAutoStartProviders>   
  7.      < add name="PreWarmMyCache" type="PreWarmCache, MyAssembly" />   
  8. < /serviceAutoStartProviders>  

The preceding serviceAutoProvider = "PreWarmMyCache" attribute references a provider in the config file.) configuration allows you to configure a custom class to encapsulate any "Push warming up" of the application) "logic. This class is automatically called before the Worker Process and Application pre-load receive any external web request, it can be used to execute any initialization or cache loading logic that you want to execute before accepting and processing the request:

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

IIS will start the application to such a State, in which it cannot accept the request until your "Push warming up)" logic is complete. After the initialization code in the Preload method is returned, the ASP. NET application is marked as a processing request.

You can also combine the New Automatic startup "warming up" feature with the IIS7 Application Request-oriented Application Request Routing, ARR) extended load balancing feature to complete Application initialization, after an HTTP request is accepted, the server sends a signal to the Load balancer. At this time, the server can put web farm to process the request.

Conclusion

ASP. NET 4 and IIS 7.5 automatically start new features provide a clearly defined way to allow you before any end users access your application, running a costly application startup and pre-Cache logic allows you to "push warmed up" the application from the very beginning, providing a consistent high-performance experience.

I hope this article will help you,

Scott

  1. Supported code snippets in Visual Studio 2010
  2. Visual Studio 2010 parallel programming and debugging diagnostic functions
  3. Illustration VS 2010 and. NET 4.0 multi-display support
  4. Multi-targeting support for VS 2010 and. NET 4
  5. Illustration VS 2010 and. NET 4 Start Project template

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.