1 //------------------------------------------------------------------------------
2 // <copyright file="SessionStateStore.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //------------------------------------------------------------------------------
6 
7 /*
8  * SessionStateStoreProviderBase
9  *
10  */
11 namespace System.Web.SessionState {
12     using System.Xml;
13     using System.Security.Permissions;
14     using System.Configuration.Provider;
15     using System.Collections.Specialized;
16 
17     [FlagsAttribute()]
18     internal enum SessionStateItemFlags : int {
19         None =                          0x00000000,
20         Uninitialized =                 0x00000001,
21         IgnoreCacheItemRemoved =        0x00000002
22     }
23 
24     [FlagsAttribute()]
25     public enum SessionStateActions : int {
26         None  =             0x00000000,
27         InitializeItem =    0x00000001
28     }
29 
30     // This interface is used by SessionStateModule to read/write the session state data
31     public abstract class SessionStateStoreProviderBase : ProviderBase {
Dispose()32         public abstract void Dispose();
33 
34         // Called by SessionStateModule to notify the provider that Session_End is defined
35         // in global.asax, and so when an item expires, it should call the expireCallback
36         // If the provider does not support session expiry, it should return false.
SetItemExpireCallback(SessionStateItemExpireCallback expireCallback)37         public abstract bool SetItemExpireCallback(SessionStateItemExpireCallback expireCallback);
38 
39         // Called at the beginning of the AcquireRequestState event
InitializeRequest(HttpContext context)40         public abstract void InitializeRequest(HttpContext context);
41 
42         // Get and return a SessionStateStoreData.
43         // Please note that we are implementing a reader/writer lock mechanism.
44         //
45         // If successful:
46         //  - returns the item
47         //
48         // If not found:
49         //  - set 'locked' to false
50         //  - returns null
51         //
52         // If the item is already locked by another request:
53         //  - set 'locked' to true
54         //  - set 'lockAge' to how long has the item been locked
55         //  - set 'lockId' to the context of the lock
56         //  - returns null
GetItem(HttpContext context, String id, out bool locked, out TimeSpan lockAge, out object lockId, out SessionStateActions actions)57         public abstract SessionStateStoreData GetItem(HttpContext context,
58                                         String id,
59                                         out bool locked,
60                                         out TimeSpan lockAge,
61                                         out object lockId,
62                                         out SessionStateActions actions);
63 
64         // Get and lock a SessionStateStoreData.
65         // Please note that we are implementing a reader/writer lock mechanism.
66         //
67         // If successful:
68         //  - set 'lockId' to the context of the lock
69         //  - returns the item
70         //
71         // If not found:
72         //  - set 'locked' to false
73         //  - returns null
74         //
75         // If the item is already locked by another request:
76         //  - set 'locked' to true
77         //  - set 'lockAge' to how long has the item been locked
78         //  - set 'lockId' to the context of the lock
79         //  - returns null
GetItemExclusive(HttpContext context, String id, out bool locked, out TimeSpan lockAge, out object lockId, out SessionStateActions actions)80         public abstract SessionStateStoreData GetItemExclusive(HttpContext context,
81                                                 String id,
82                                                 out bool locked,
83                                                 out TimeSpan lockAge,
84                                                 out object lockId,
85                                                 out SessionStateActions actions);
86 
87         // Unlock an item locked by GetExclusive
88         // 'lockId' is the lock context returned by previous call to GetExclusive
ReleaseItemExclusive(HttpContext context, String id, object lockId)89         public abstract void ReleaseItemExclusive(HttpContext context,
90                                                     String id,
91                                                     object lockId);
92 
93         // Write an item.
94         // Note: The item is originally obtained by GetExclusive
95         // Because SessionStateModule will release (by ReleaseExclusive) am item if
96         // it has been locked for too long, so it is possible that the request calling
97         // Set() may have lost the lock to someone else already.  This can be
98         // discovered by comparing the supplied lockId with the lockId value
99         // stored with the state item.
SetAndReleaseItemExclusive(HttpContext context, String id, SessionStateStoreData item, object lockId, bool newItem)100         public abstract void SetAndReleaseItemExclusive(HttpContext context,
101                                     String id,
102                                     SessionStateStoreData item,
103                                     object lockId,
104                                     bool newItem);
105 
106         // Remove an item.  See the note in Set.
RemoveItem(HttpContext context, String id, object lockId, SessionStateStoreData item)107         public abstract void RemoveItem(HttpContext context,
108                                         String id,
109                                         object lockId,
110                                         SessionStateStoreData item);
111 
112         // Reset the expire time of an item based on its timeout value
ResetItemTimeout(HttpContext context, String id)113         public abstract void ResetItemTimeout(HttpContext context, String id);
114 
115         // Create a brand new SessionStateStoreData. The created SessionStateStoreData must have
116         // a non-null ISessionStateItemCollection.
CreateNewStoreData(HttpContext context, int timeout)117         public abstract SessionStateStoreData CreateNewStoreData(HttpContext context, int timeout);
118 
CreateUninitializedItem(HttpContext context, String id, int timeout)119         public abstract void CreateUninitializedItem(HttpContext context, String id, int timeout);
120 
121         // Called during EndRequest event
EndRequest(HttpContext context)122         public abstract void EndRequest(HttpContext context);
123 
Initialize(string name, NameValueCollection config, IPartitionResolver partitionResolver)124         internal virtual void Initialize(string name, NameValueCollection config, IPartitionResolver partitionResolver) {
125         }
126     }
127 
128     public class SessionStateStoreData {
129         ISessionStateItemCollection      _sessionItems;
130         HttpStaticObjectsCollection _staticObjects;
131         int                         _timeout;
132 
SessionStateStoreData(ISessionStateItemCollection sessionItems, HttpStaticObjectsCollection staticObjects, int timeout)133         public SessionStateStoreData(ISessionStateItemCollection sessionItems,
134                                     HttpStaticObjectsCollection staticObjects,
135                                     int timeout) {
136             _sessionItems = sessionItems;
137             _staticObjects = staticObjects;
138             _timeout = timeout;
139         }
140 
141         virtual public ISessionStateItemCollection Items {
142             get {
143                 return _sessionItems;
144             }
145         }
146 
147         virtual public HttpStaticObjectsCollection StaticObjects {
148             get {
149                 return _staticObjects;
150             }
151         }
152 
153         virtual public int Timeout {
154             get {
155                 return _timeout;
156             }
157 
158             set {
159                 _timeout = value;
160             }
161         }
162     }
163 }
164