2 // System.Web.HttpApplicationState
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System.Threading;
30 using System.Collections.Specialized;
31 using System.Security.Permissions;
35 // CAS - no InheritanceDemand here as the class is sealed
36 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
37 public sealed class HttpApplicationState : NameObjectCollectionBase
39 HttpStaticObjectsCollection _AppObjects;
40 HttpStaticObjectsCollection _SessionObjects;
42 ReaderWriterLockSlim _Lock;
44 internal HttpApplicationState ()
46 _Lock = new ReaderWriterLockSlim ();
49 internal HttpApplicationState (HttpStaticObjectsCollection AppObj, HttpStaticObjectsCollection SessionObj)
52 _SessionObjects = SessionObj;
53 _Lock = new ReaderWriterLockSlim ();
57 get { return _Lock.IsReadLockHeld || _Lock.IsWriteLockHeld; }
60 public void Add (string name, object value)
62 bool acquired = false;
65 _Lock.EnterWriteLock ();
68 BaseAdd (name, value);
70 if (acquired && IsLockHeld)
71 _Lock.ExitWriteLock ();
77 bool acquired = false;
80 _Lock.EnterWriteLock ();
85 if (acquired && IsLockHeld)
86 _Lock.ExitWriteLock ();
90 public object Get (string name)
93 bool acquired = false;
96 _Lock.EnterReadLock ();
101 if (acquired && IsLockHeld)
102 _Lock.ExitReadLock ();
108 public object Get (int index)
110 bool acquired = false;
113 _Lock.EnterReadLock ();
116 return BaseGet (index);
118 if (acquired && IsLockHeld)
119 _Lock.ExitReadLock ();
123 public string GetKey (int index)
125 bool acquired = false;
128 _Lock.EnterReadLock ();
131 return BaseGetKey (index);
133 if (acquired && IsLockHeld)
134 _Lock.ExitReadLock ();
140 if (!_Lock.IsWriteLockHeld)
141 _Lock.EnterWriteLock ();
144 public void Remove (string name)
146 bool acquired = false;
149 _Lock.EnterWriteLock ();
154 if (acquired && IsLockHeld)
155 _Lock.ExitWriteLock ();
159 public void RemoveAll ()
164 public void RemoveAt (int index)
166 bool acquired = false;
169 _Lock.EnterWriteLock ();
172 BaseRemoveAt (index);
174 if (acquired && IsLockHeld)
175 _Lock.ExitWriteLock ();
179 public void Set (string name, object value)
181 bool acquired = false;
184 _Lock.EnterWriteLock ();
187 BaseSet (name, value);
189 if (acquired && IsLockHeld)
190 _Lock.ExitWriteLock ();
194 public void UnLock ()
196 if (_Lock.IsWriteLockHeld)
197 _Lock.ExitWriteLock ();
200 public string [] AllKeys {
202 bool acquired = false;
205 _Lock.EnterReadLock ();
208 return BaseGetAllKeys ();
210 if (acquired && IsLockHeld)
211 _Lock.ExitReadLock ();
216 public HttpApplicationState Contents {
220 public override int Count {
222 bool acquired = false;
225 _Lock.EnterReadLock ();
230 if (acquired && IsLockHeld)
231 _Lock.ExitReadLock ();
236 public object this [string name] {
237 get { return Get (name); }
238 set { Set (name, value); }
241 public object this [int index] {
242 get { return Get (index); }
245 // ASP Session based objects
246 internal HttpStaticObjectsCollection SessionObjects {
248 if (_SessionObjects == null)
249 _SessionObjects = new HttpStaticObjectsCollection ();
251 return _SessionObjects;
255 // ASP App based objects
256 public HttpStaticObjectsCollection StaticObjects {
258 if (_AppObjects == null)
259 _AppObjects = new HttpStaticObjectsCollection ();