Application Sharing data

Source: Internet
Author: User
Tags allkeys

1. The difference between application and session

    • Application object: Implements data sharing at the program level.
    • Session object: Enables data sharing at the sessions level.

You can use the Application object when you want to share information at the entire program level. For example, you need to design a counter to count all the people accessing the system, or to record time at the beginning and end of the program. Can be implemented using application objects. The key in the Application object is a string, and the value is the type of object, that is, you can store any type of data into a key in application, similar to Hashtable, except to convert the type when it is taken.

2, Application Introduction

Application state application is derived from NameObjectCollectionBase, allowing the user to save or obtain the corresponding object by the name of a string.

The Application object is an instance of the HttpApplicationState class. Its life cycle starts when the system starts running and terminates at System shutdown. Therefore, when not in use, the time to release space.

HttpApplicationState is defined as follows:

Class Httpapplicationstate:nameobjectcollectionbase

For application state management objects, because all requests may require access to this object, in order to solve the problem of concurrent access, it also provides a thread synchronization mechanism to synchronize access to the collection through a lock.

    • public void Lock ()
    • public void UnLock ()

The HttpApplicationState class uses the AllKeys and Count properties and the Add,clear,get,getkey,remove,removeat, set methods to perform automatic locking and unlocking. However, if you need to have multiple successive accesses to the data saved in the global application state, locking and unlock through the displayed lock can help improve access efficiency.

Take a look at the features available in MSDN application:

Method:

Add adds a new object to the HttpApplicationState collection.

Baseadd adds an item with the specified key and value to the NameObjectCollectionBase instance. (inherited from NameObjectCollectionBase.) )

Baseclear Remove all items from the NameObjectCollectionBase instance. (inherited from NameObjectCollectionBase.) )

Baseget is overloaded. Basegetallkeys returns a String array that contains all the keys in the NameObjectCollectionBase instance. (inherited from NameObjectCollectionBase.) )

Basegetallvalues is overloaded.

Basegetkey gets the key of the key at the specified index of the NameObjectCollectionBase instance. (inherited from NameObjectCollectionBase.) )

Basehaskeys gets a value that indicates whether the NameObjectCollectionBase instance contains an entry for which the key is not a nullnothingnullptrnull reference (nothing in Visual Basic). (inherited from NameObjectCollectionBase.) )

Baseremove removes the item with the specified key in the NameObjectCollectionBase instance. (inherited from NameObjectCollectionBase.) )

Baseremoveat removes the item at the specified index of the NameObjectCollectionBase instance. (inherited from NameObjectCollectionBase.) )

Baseset is overloaded.

Clear removes all objects from the HttpApplicationState collection.

Get is overloaded. Gets the HttpApplicationState object by name or index.

GetEnumerator Returns an enumerator that iterates through the nameobjectcollectionbase. (inherited from NameObjectCollectionBase.) )

GetKey gets the HttpApplicationState object name by index.

The GetObjectData implements the ISerializable interface and returns the data required to serialize the NameObjectCollectionBase instance. (inherited from NameObjectCollectionBase.) )

Lock locks access to the HttpApplicationState variable to facilitate access synchronization. The ondeserialization implements the ISerializable interface and raises the deserialization event after deserialization is complete. (inherited from NameObjectCollectionBase.) )

Remove removes the named object from the HttpApplicationState collection.

RemoveAll remove all objects from the HttpApplicationState collection.

RemoveAt removes a HttpApplicationState object from the collection by index.

Set updates the object values in the HttpApplicationState collection.

UnLock unlocks access to the HttpApplicationState variable to facilitate access synchronization.

Property:

AllKeys gets the access key in the HttpApplicationState collection.

Contents gets a reference to the HttpApplicationState object.

Count gets the number of objects in the HttpApplicationState collection. (Rewrite NameObjectCollectionBase..::.) Count. )

IsReadOnly Gets or sets a value that indicates whether the NameObjectCollectionBase instance is read-only. (inherited from NameObjectCollectionBase.) )

Item is overloaded. Gets access to the objects in the HttpApplicationState collection. Overloads the property to allow access to the object by name or numeric index.

Keys get nameobjectcollectionbase.::. An instance of Keyscollection that contains all the keys in the NameObjectCollectionBase instance. (inherited from NameObjectCollectionBase.) )

StaticObjects gets all the objects declared by the <object> tag, where the scope is set to "Application" in the ASP.

  3. Global.asax File

The Global.asax file is closely related to the Application object, and its main function is to set up some variables to be used at the program level and to implement OnStart and OnEnd events for application objects.

Example: Global.asax code:

        void Application_Start (object sender, EventArgs e) {application.ad D ( "one " Liu Bei  ); Application.add ( "two ", "  Guan Yu " ); Application.add ( "three "  Zhang Fei  "); } 

Page code:

        Protectedvoid Page_Load (Objectsender, EventArgs e) {string[] Strarr = Application.allkeys;//Gets a string array of all key componentsforeach (String strInchStrarr) {Response.Write (str +"");//Output one, one, three} HttpApplicationState has = application.contents;//Gets a reference to the global object so that the global object can be processed in other pages, not in Global.asax. Has. ADD ("Four","Zhao Yun");//Gets a reference to the global object, and then adds more than one object, which is also added to the application, and can be accessed everywhere.int count =Application.count; Response.Write (count);//Output 4 has added more than one Zhao Yunforeach (String keyInchApplication.keys) {Response.Write (key +":"+ Application[key] +"");//Output one: Liu Bei: Guan Yu three: Zhang Fei four: Zhao Yun} Response.Write ("<br/>"); Application.add ("Fine","Jack Huang");//Add an Object Application.removeat (4);//Remove an object by index number//Index number starts from 0 application.remove ("Four");//Remove an object by keyforeach (String keyInchApplication.allkeys) {Response.Write (Application.get (key));// output Liu Bei Guan Yu Zhang The Get method can get the value by string, index number } application.set ( "one"  " Zhuge Liang  "); // "one"]); // output Zhuge Liang, you can see that has been modified 1); // by index number, get Key name Response.Write (strkey); // output,"           

Application Sharing data

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.