ASP. NET application

Source: Internet
Author: User

Application lifetime
An ASP. NET application is created when the server is requested for the first time. No ASP. NET code is executed before that. After accepting the first request, an httpapplication instance pool is created and the application_onstart event is activated. The httpapplication instance processes the request and subsequent requests until the final instance exits and then triggers the application_onend event.
Note that the init and dispose methods of httpapplication are called in each instance. Therefore, the events of application_onstart and application_onend are called multiple times. In an ASP. NET application, only these events are shared among all httpapplication instances.

Multithreading

If we use objects in the application range type, we should note that ASP. NET processes requests concurrently, so that the application object can be accessed by multiple threads. Therefore, if the Code in the following example is accessed by different clients at the same time, it is dangerous and may not achieve the intended purpose. For ease of comparison, we listed the code written in three languages: VB, C #, and JScript:
C #
<%
Application ["counter"] = (int32) application ["counter"] + 1;
%>
VB
<%
Application ("counter") = ctype (Application ("counter") + 1, int32)
%>

To implement secure thread processing, we can use the lock and Unlock methods to process this application object. See the following code written in three languages: VB, C #, and JScript:
C #
<%
Application. Lock ();
Application ["counter"] = (int32) application ["counter"] + 1;
Application. Unlock ();
%>

VB
<%
Application. Lock ()
Application ("counter") = ctype (Application ("counter") + 1, int32)
Application. Unlock ()
%>

File global. asax
In addition to writing the UI (use interface: User Interface), we can also add "application"-level control logic code and event-triggered programs in the Web application. These codes do not operate and generate the UI, and basically do not respond to individual page requests. They are responsible for handling high-level application events, including application_start, application_end, session_start, and session_end. These control logic codes are stored on the Web

In the global. asax file under the root directory of the virtual directory where the application is located, ASP. NET automatically parses the file and compiles it into a dynamic. NET Framework class. This class extends the base class of httpapplication. When any resource or URL in the application namespace is accessed for the first time, it is created.

When the global. asax file is set, any direct URL request for the Global. asax file will be automatically rejected, thus preventing external users from downloading it and browsing its content.

Events within the scope of application or session

You can write a method in the global. Asa file to define an event trigger program for the HTTP application base class. The method name must match the string "application _ event name ". For example, see the following code written in three languages: VB, C #, and JScript:

C #
<Script language = "C #" runat = "server">
Void application_start (){
// Application startup code goes here
}
</SCRIPT>

VB
<Script language = "VB" runat = "server">
Sub application_start ()
'Application startup code goes here
End sub
</SCRIPT>

If you need to enter another namespace for the event triggering code, you can write an input identifier similar to the following code on the. ASPX page:
<% @ Import namespace = "system. Text" %>
The following example illustrates the lifetime of the application, session, and request:

 

 

When the page is opened for the first time, the onstart event of the application and session will be activated. For the event-triggered programs, see the code written in the following three languages: VB, C #, and JScript:

C #
Void application_start (){
Response. Write ("application is starting ...");
}
Void session_start (){
Response. Write ("session is starting ...");
Session. Timeout = 1;
}

VB

Sub application_start ()
Response. Write ("application is starting ...")
End sub
Sub session_start ()
Response. Write ("session is starting ...")
Session. Timeout = 1
End sub

The beginrequest and endrequest events are activated each time a request occurs. For example, when a page is refreshed

And the page_load method. Note: When the current session is abandoned (click the "End this session" button), a new session is generated and the session_onstart event is triggered again.

 

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.