1 //------------------------------------------------------------------------------
2 // <copyright file="httpapplicationstate.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 //------------------------------------------------------------------------------
8 * Application State Dictionary class
10 * Copyright (c) 1999 Microsoft Corporation
13 namespace System.Web {
14 using System.Threading;
15 using System.Runtime.InteropServices;
16 using System.Security.Permissions;
18 using System.Collections;
19 using System.Collections.Specialized;
21 using System.Web.Util;
24 // Application state collection
30 /// The HttpApplicationState class enables developers to
31 /// share global information across multiple requests, sessions, and pipelines
32 /// within an ASP.NET application. (An ASP.NET application is the sum of all files, pages,
33 /// handlers, modules, and code
34 /// within the scope of a virtual directory and its
35 /// subdirectories on a single web server).
38 public sealed class HttpApplicationState : NameObjectCollectionBase {
39 // app lock with auto-unlock feature
40 private HttpApplicationStateLock _lock = new HttpApplicationStateLock();
42 // static object collections
43 private HttpStaticObjectsCollection _applicationStaticObjects;
44 private HttpStaticObjectsCollection _sessionStaticObjects;
46 internal HttpApplicationState() : this(null, null) {
49 internal HttpApplicationState(HttpStaticObjectsCollection applicationStaticObjects,
50 HttpStaticObjectsCollection sessionStaticObjects)
51 : base(Misc.CaseInsensitiveInvariantKeyComparer) {
52 _applicationStaticObjects = applicationStaticObjects;
54 if (_applicationStaticObjects == null)
55 _applicationStaticObjects = new HttpStaticObjectsCollection();
57 _sessionStaticObjects = sessionStaticObjects;
59 if (_sessionStaticObjects == null)
60 _sessionStaticObjects = new HttpStaticObjectsCollection();
64 // Internal accessor to session static objects collection
67 internal HttpStaticObjectsCollection SessionStaticObjects {
68 get { return _sessionStaticObjects;}
72 // Implementation of standard collection stuff
78 /// the number of item objects in the application state collection.</para>
80 public override int Count {
101 /// a new state object to the application state collection.
104 public void Add(String name, Object value) {
105 _lock.AcquireWrite();
107 BaseAdd(name, value);
110 _lock.ReleaseWrite();
116 /// <para>Updates an HttpApplicationState value within the collection.</para>
118 public void Set(String name, Object value) {
119 _lock.AcquireWrite();
121 BaseSet(name, value);
124 _lock.ReleaseWrite();
132 /// object from the application state collection by name.</para>
134 public void Remove(String name) {
135 _lock.AcquireWrite();
140 _lock.ReleaseWrite();
148 /// object from the application state collection by name.</para>
150 public void RemoveAt(int index) {
151 _lock.AcquireWrite();
156 _lock.ReleaseWrite();
164 /// all objects from the application state collection.
167 public void Clear() {
168 _lock.AcquireWrite();
173 _lock.ReleaseWrite();
181 /// all objects from the application state collection.
184 public void RemoveAll() {
193 /// Enables user to retrieve application state object by name.
196 public Object Get(String name) {
212 /// a user to add/remove/update a single application state object.</para>
214 public Object this[String name]
216 get { return Get(name);}
217 set { Set(name, value);}
226 /// to retrieve a single application state object by index.
229 public Object Get(int index) {
234 obj = BaseGet(index);
246 /// Enables user to retrieve an application state object name by index.
249 public String GetKey(int index) {
253 s = BaseGetKey(index);
266 /// user to retrieve an application state object by index.
269 public Object this[int index]
271 get { return Get(index);}
274 // access to keys and values as arrays
280 /// to retrieve all application state object names in collection.
283 public String[] AllKeys {
285 String [] allKeys = null;
289 allKeys = BaseGetAllKeys();
306 /// Returns "this". Provided for legacy ASP compatibility.
309 public HttpApplicationState Contents {
316 /// Exposes all objects declared via an <object
317 /// runat=server></object> tag within the ASP.NET application file.
320 public HttpStaticObjectsCollection StaticObjects {
321 get { return _applicationStaticObjects;}
332 /// access to all application state variables. Facilitates access
337 _lock.AcquireWrite();
343 /// Unocks access to all application state variables. Facilitates access
347 public void UnLock() {
348 _lock.ReleaseWrite();
351 internal void EnsureUnLock() {
352 _lock.EnsureReleaseWrite();
358 // Recursive read-write lock that allows removing of all
359 // outstanding write locks from the current thread at once
361 internal class HttpApplicationStateLock : ReadWriteObjectLock {
362 private int _recursionCount;
363 private int _threadId;
365 internal HttpApplicationStateLock() {
368 internal override void AcquireRead() {
369 int currentThreadId = SafeNativeMethods.GetCurrentThreadId();
371 if (_threadId != currentThreadId)
372 base.AcquireRead(); // only if no write lock
375 internal override void ReleaseRead() {
376 int currentThreadId = SafeNativeMethods.GetCurrentThreadId();
378 if (_threadId != currentThreadId)
379 base.ReleaseRead(); // only if no write lock
382 internal override void AcquireWrite() {
383 int currentThreadId = SafeNativeMethods.GetCurrentThreadId();
385 if (_threadId == currentThreadId) {
390 _threadId = currentThreadId;
395 internal override void ReleaseWrite() {
396 int currentThreadId = SafeNativeMethods.GetCurrentThreadId();
398 if (_threadId == currentThreadId) {
399 if (--_recursionCount == 0) {
407 // release all write locks held by the current thread
410 internal void EnsureReleaseWrite() {
411 int currentThreadId = SafeNativeMethods.GetCurrentThreadId();
413 if (_threadId == currentThreadId) {