Compartir a través de


Application Object (IIS)

You can use the Application object to share information among all users of a given application. An ASP-based application is defined as all the .asp files in a virtual directory and its subdirectories. Because the Application object can be shared by more than one user, there are Lock and Unlock methods to ensure that multiple users do not try to alter a property simultaneously.

Methods

The Application object defines the following methods.

Method

Description

Application.Contents.Remove

Deletes an item from the Application object's Contents collection.

Application.Contents.RemoveAll

Deletes all items from the Application object's Contents collection.

Application.Lock

Prevents other clients from modifying Application object properties.

Application.Unlock

Allows other clients to modify Application object properties.

Properties

The Application object defines the following properties.

Property

Description

Application.Contents Collection

Contains all of the items that have been added to the application through script commands.

Application.StaticObjects Collection

Contains all of the objects added to the session with the <OBJECT> tag.

Events

The Application object defines the following events.

Event

Description

Application_OnEnd Event

Occurs when the application quits, after the Session_OnEnd event. Only the Application Object and Server Object built-in objects are available.

Application_OnStart Event

Occurs before the first new session is created, that is, before the Session_OnStart event. Only the Application Object and Server Object built-in objects are available. Referencing the Session Object, Request Object, or Response Object objects in the Application_OnStart event script causes an error.

Remarks

You can store values in the Application Collections. Information stored in the Application collections is available throughout the application and has application scope. The following script demonstrates storage of two types of variables.

<% 
Application("greeting") = "Welcome to My Web World!" 
Application("num") = 25 
%> 

Each of these variables are members of the Application.Contents Collection.

You can also assign a component instance to a variable that has application scope. If you assign a component instance to a variable with the Server.CreateObject method, the variable will be a member of the Application.Contents collection. If the variable is assigned with the <OBJECT> tag, the variable will be a member of the Application.StaticObjects Collection.

You should be careful about assigning component instances to variables with application scope because some components are not designed to be given application scope. For more information, see the Platform Software Development Kit (SDK).

If you assign a component instance to a variable in the Application.Contents Collection and use Microsoft ? Visual Basic ? Scripting Edition (VBScript) as your primary scripting language, you must use the Set keyword, as illustrated in the following script.

<% Set Application("Obj1") = Server.CreateObject("MyComponent") %> 

You can then reference the methods and properties of MyComponent on subsequent Web pages by using the following script:

<% Application("Obj1").MyObjMethod %> 

Or you can reference the methods and properties of MyComponent on subsequent Web pages by extracting a local copy of the object and using the following script:

<%  
Set MyLocalObj1 = Application("Obj1")  
MyLocalObj1.MyObjMethod 
%> 

Another way to create objects with application scope is by using the <OBJECT> tag in the Global.asa file. For more information, see Global.asa Syntax.

You cannot store a built-in object in the Application object. For example, each of the following lines returns an error.

<% 
Set Application("var1") = Session 
Set Application("var2") = Request 
Set Application("var3") = Response 
Set Application("var4") = Server 
Set Application("var5") = Application 
Set Application("var6") = ObjectContext 
%> 

You should be aware of the threading model used by any components you give application scope. The threading model used to develop the component will have a significant impact on whether a component instance should be assigned to a variable in one of the Application collections.

For more information, see Setting the Scope of COM Objects in ASP Pages. For code examples of COM components that can be called from ASP, see Creating COM Components for ASP.

If you store an array in an Application object, you should not attempt to alter the elements of the stored array directly. For example, the following script does not work:

<% Application("StoredArray")(3) = "new value" %> 

The preceding script does not work because the Application object is implemented as a collection. The array element StoredArray(3) does not receive the new value. Instead, the value is included in the Application object collection and overwrites any information that had previously been stored at that location.

It is strongly recommended that if you store an array in the Application object, you retrieve a copy of the array before retrieving or changing any of the elements of the array. When you are done with the array, you should store the array in the Application object again, so that any changes you made are saved, as shown in the following scripts.

--- File1.asp --- 
<% 
'Creating and initializing the array. 
dim MyArray() 
Redim MyArray(5) 
MyArray(0) = "hello" 
MyArray(1) = "some other string" 

'Storing the array in the Application object. 
Application.Lock 
Application("StoredArray") = MyArray 
Application.Unlock 

Server.Transfer("file2.asp") 
%> 

--- File2.asp --- 
<% 
'Retrieving the array from the Application Object 
'and modifying its second element. 
LocalArray = Application("StoredArray") 
LocalArray(1) = " there" 

'Printing out the string "hello there." 
Response.Write(LocalArray(0)&LocalArray(1)) 

'Re-storing the array in the Application object. 
'This overwrites the values in StoredArray with the new values. 
Application.Lock 
Application("StoredArray") = LocalArray 
Application.Unlock 
%>

Example Code

The following example uses the application variable NumVisits to store the number of times that a particular page has been accessed. The Lock method is called to ensure that only the current client can access or alter NumVisits. Calling the Unlock method then enables other users to access the Application object.

<% 
Application.Lock 
Application("NumVisits") = Application("NumVisits") + 1 
Application.Unlock 
%>  

This application page has been visited  
<%= Application("NumVisits") %> times! 

Requirements

Client: Requires Windows XP Professional, Windows 2000 Professional, or Windows NT Workstation 4.0.

Server: Requires Windows Server 2003, Windows 2000 Server, or Windows NT Server 4.0.

Product: IIS