Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the automatic start feature of ASP.NET 4

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "how to use the auto-startup feature of ASP.NET 4". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use the auto-startup feature of ASP.NET 4.

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 work (this event is triggered when * requests are executed). They can either design custom scripts to periodically send fake requests to the application to "wake it up" to execute the code before the customer visits, or just let the unfortunate visitors wait for the logic to finish before processing their requests (which can cause long delays for these users).

A new feature in ASP.NET 4 called "auto-start" is a good solution to this scenario and can be used when running ASP.NET 4 on IIS 7.5 (released with Windows 7 and Windows Server 2008 R2). This auto-startup feature provides a controlled way to start an application worker process, initialize ASP.NET applications, and then accept HTTP requests.

Configure an ASP.NET 4 application to start automatically

To use the ASP.NET 4 auto-startup feature, you first configure the IIS application pool worker process so that the applications running in it start automatically when the web server is loaded. The configuration method is to open the applicationHost.config file of IIS 7.5 (C:\ Windows\ System32\ inetsrv\ config\ applicationHost.config) and click on the appropriate

< applicationPools>

Add a startMode= "AlwaysRunning" attribute to the

< 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 save the startMode property changes of the applicationHost.config file, you will see a new "w3wp.exe" worker process started immediately after the file is saved.

A single IIS application pool worker process can host multiple ASP.NET applications, and you can use the

< application>

Add a 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 configuration in the config file, allowing you to configure a custom class that encapsulates any "warming up" logic of the application. This class is automatically called 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 loading logic that you want to perform before accepting and processing the request:

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

IIS will launch the application to a state where it cannot accept requests until your "warming up" logic is complete. After the initialization code in the Preload method is returned, the ASP.NET application is marked as ready to process the request.

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

At this point, I believe you have a deeper understanding of "how to use the automatic startup feature of ASP.NET 4". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report