ArticleDirectory
- Static Application variables
ApplicationProgramStatus allows you to save global objects accessed by all customers. The application status is based onSystem. Web. httpapplicationstateClass in the web page through the built-inApplicationObject.
For example, you can create a global. asax event handler to track the number of sessions created, or use the same logic to track the number of visits to a page:
Protected VoidPage_load (ObjectSender, eventargs E)
{
IntCount = 0;
If(Application ["Hitcounterfororderpage"]! =Null)
{
Count = (Int) Application ["Hitcounterfororderpage"];
Count ++;
Application ["Hitcounterfororderpage"] = Count;
}
}
The application status is saved as a status item of the object type. The values in all sets need to be converted to the type. The project in the application state never expires and is saved to the application or the server for restart.
The application status is not frequently used, because the efficiency is not high. In the previous example, the counter numbers are not accurate. When a large number of users access the counter at the same time, the count is lost. To avoid this problem, you can use the lock () and unlock () methods to prevent users from simultaneously accessing the application set:
Protected VoidPage_load (ObjectSender, eventargs E)
{
Application. Lock ();
IntCount = 0;
If(Application ["Hitcounterfororderpage"]! =Null)
{
Count = (Int) Application ["Hitcounterfororderpage"];
Count ++;
Application ["Hitcounterfororderpage"] = Count;
}
Application. Unlock ();
}
Unfortunately, all users requesting this page will be suspended until the application set is released. This will greatly reduce performance. Generally, changing values are not suitable for application status.
In fact, ASP. NET rarely uses the application state, because its two most common functions have been replaced by simpler and more effective methods:
- Previously,People use the application state to save constants at the application level, such as database connection strings. Currently, such constants can be stored in Web. config, which is usually more flexible, because it is easy to modify and does not need to be changed.CodeOr re-compile the application..
- The application status can also be used to save frequently-used but time-consuming information, such as querying all product directories in the database. But now it is more similar. It is wise to save common information in the ASP. NET cache. Many applications in application status can be effectively replaced by Cache.
Static Application variables
You can also addStatic member variable:
PublicStaticString [] filelist;
The key to this function is static variable. ASP. NET creates a connection pool of the httpapplication class to serve multiple requests. In this way, each request may be served by different httpapplication objects. Each httpapplication object has its own instance data. However, there is only one copy of static data.
There is still the possibility that multiple pages will call this static variable, but because it is not an application object, there is no automatic lock, therefore, use the C # Lock statement to temporarily limit variables to a separate thread..
Private StaticDictionary <String,String> Metadata =NewDictionary <String,String> ();
Public VoidAddmetadata (StringKey,String Value)
{
Lock(Metadata)
{
Metadata [Key] =Value;
}
}
Public StringGetmetadata (StringKey)
{
Lock(Metadata)
{
ReturnMetadata [Key];
}
}
Using static member variables instead of application sets has two advantages:.
- First, when the value is accessed or modified (by binding data to the attribute process or method), it allows you to write code that can run automatically. You can use this code to record the number of times a value has been accessed, check whether the data is still valid, or whether to create a new one. This example usesDelayed initialization ModeAnd only create a local object in the first request:
Private Static String[] Filelist;
Public Static String[] Filelist
{
Get
{
If(Filelist =Null)
{
Filelist = directory. getfiles (httpcontext. Current. Request. physicalapplicationpath );
}
ReturnFilelist;
}
}
// This example uses the file listing class to read the file list of the Web application.
// This function cannot be implemented through the application set
- Another benefit of using static member variables is type security. The variable types obtained from the Global class do not need to be converted.