Asp.net internal object Application

Source: Internet
Author: User

1. Application Object


Application description
In the asp.net environment, the Application object comes from the HttpApplictionStat class. It can share public information between multiple requests and connections, or act as an information transmission pipeline between request connections. Use the Application object to save the variables we want to pass. Because the Application object is valid throughout the life cycle of the Application, it can be accessed on different pages, just as convenient as using global variables. If you use communication between multiple users, you can use the website calculator to survive the entire application running cycle. static acts on the entire application running cycle.

 


Application Creation
[Csharp] view plaincopyprint?
Application ["application name"] = "application Value ";

Application ["application name"] = "application Value"; Application Usage

[Csharp] view plaincopyprint?
String str = Application ["application name"];

String str = Application ["application name"];

 

Common attributes and Methods
All
Returns all Application object variables to an array of objects.
 
AllKeys
Returns all Application object variables to a string array.
 
Count
Get the number of object variables in Application
 
Item
Content value returned by the Application variable name
 
Add
Add an Application variable value
 
Clear
Clear all Application variable values
 
Get
Variable value returned by the variable name
 
Set
Update Application variable value
 
Lock
Lock all Application variable values
 
UnLock
Unlock Application variable
 

 

Application Object Features:
Physical storage location: Server Memory.

Storage type restrictions: any type.

Status range: the entire application.

Storage size limit: any size.

Life cycle: the application is created at the beginning (precisely the first time the user requests a URL) and destroyed at the end of the application.

Security and Performance: data is always stored on the server, which is highly secure but not easy to store too much data.

Advantages and disadvantages and precautions: Data retrieval is fast, but data is not automatically released due to lack of self-management mechanism.

 

 

2. Application Lifecycle


Several stages of ASP. NET application lifecycle:

1. the user requests application resources from the Web server.

2. ASP. NET receives the first request to the application.

3. Create an ASP. NET core object for each request.

4. Assign the HttpApplication object to the request.

5. requests are processed by the HttpApplication pipeline.

 


Application lifecycle events and Global. asax files
During the life cycle of an application, the application triggers events that can be processed and calls specific methods that can be rewritten. To process application events or methods, you can create a file named Global. asax in the application root directory.

If the Global. asax file is created, ASP. NET will compile it into a class derived from the HttpApplication class, and then use this derived class to represent the application.

An instance of the HttpApplication process processes only one request at a time. Since non-static members in the application class do not need to be locked, this can simplify the event processing process of the application. In this way, the requested data can be stored in non-static members of the application class. For example, you can define an attribute in the Global. asax file and assign a request-specific value to the attribute.

By using the naming convention Application_event (such as Application_BeginRequest), ASP. NET can automatically bind application events to the handler in the Global. asax file. This is similar to automatically binding an ASP. NET page method to an event (such as a Page_Load event of a page. The Application_Start and Application_End methods are special methods that do not represent the HttpApplication event. ASP. NET only calls these methods once during the lifecycle of an application domain, rather than once for each HttpApplication instance.

The following table lists the events and methods used during the application lifecycle. These events are far more than listed, but these events are the most commonly used.

Events or methods
Description
 
Application_Start
This API is called when you request the first resource (such as a page) in an ASP. NET application. The Application_Start method is called only once during the application lifecycle. You can use this method to start a task, such as loading data into the cache and initializing static values.

Only static data should be set during application startup. Because instance data can only be used by the first instance of the created HttpApplication class, do not set any instance data.
 
Application _ event
When appropriate in the application lifecycle, see the content listed in the application lifecycle table before this topic.

Application_Error can be triggered at any stage of the application lifecycle.

Because requests are short-circuited, Application_EndRequest is the only event that can be triggered during each request. For example, if two modules handle the Application_BeginRequest event, the first module raises an exception and does not call the Application_BeginRequest event for the second module. However, the Application_EndRequest method is always called to clear the resources of the application.
 
HttpApplication. Init
After all modules are created, each instance of the HttpApplication class is called once.
 
Dispose
Called before the application instance is destroyed. You can use this method to manually release any unmanaged resources.
 
Application_End
Each application lifecycle is called once before the application is uninstalled.
 

 

 

 

3. Use the global. asax file and the Application object to implement simple website counting:
Global. asax file:

 

[Csharp]
<% @ Application Language = "C #" %>
 
<Script runat = "server">
 
Void Application_Start (object sender, EventArgs e)
{
// Code that runs when the application starts
Application ["count"] = 0; // The initial count starts from 0.
}

Void Application_End (object sender, EventArgs e)
{
// Code that runs when the application is closed
 
}

Void Application_Error (object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
 
}
 
Void Session_Start (object sender, EventArgs e)
{
// The code that runs when the new session starts
Application. Lock (); // synchronous to avoid simultaneous write
Application ["count"] = (int) Application ["count"] + 1; // No session is established. This global variable is added with 1
Application. UnLock (); // synchronization ends
 
}
 
Void Session_End (object sender, EventArgs e)
{
// The code that runs when the session ends.
// Note: The Session_End event is triggered only when the sessionstate mode in the Web. config file is set to InProc.
// If the session mode is set to StateServer
// Or SQLServer, the event is not triggered.
 
}

</Script>

<% @ Application Language = "C #" %>

<Script runat = "server">

Void Application_Start (object sender, EventArgs e)
{
// Code that runs when the application starts
Application ["count"] = 0; // The initial count starts from 0.
}

Void Application_End (object sender, EventArgs e)
{
// Code that runs when the application is closed

}

Void Application_Error (object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs

}

Void Session_Start (object sender, EventArgs e)
{
// The code that runs when the new session starts
Application. Lock (); // synchronous to avoid simultaneous write
Application ["count"] = (int) Application ["count"] + 1; // No session is established. This global variable is added with 1
Application. UnLock (); // synchronization ends

}

Void Session_End (object sender, EventArgs e)
{
// The code that runs when the session ends.
// Note: The Session_End event is triggered only when the sessionstate mode in the Web. config file is set to InProc.
// If the session mode is set to StateServer
// Or SQLServer, the event is not triggered.

}

</Script>
Page code:


[Csharp]
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
 
Public partial class login: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{

Response. Write ("Welcome! ");
Response. Write ("you are the first ");
Response. Write (int) Application ["count"]);
Response. Write ("person accessing this website! ");

}


}

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;

Public partial class login: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{

Response. Write ("Welcome! ");
Response. Write ("you are the first ");
Response. Write (int) Application ["count"]);
Response. Write ("person accessing this website! ");

}


}

 

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.