IHttpSessionState 接口
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
定义协定以实现自定义会话状态容器。
public interface class IHttpSessionState
public interface IHttpSessionState
type IHttpSessionState = interface
Public Interface IHttpSessionState
- 派生
示例
下面的代码示例实现 IHttpSessionState 接口,以创建新的会话状态容器类,名为 MySessionState
。
using System;
using System.Web;
using System.Web.SessionState;
using System.Collections;
using System.Collections.Specialized;
using System.Text;
using System.Threading;
using System.Globalization;
namespace Samples.AspNet.SessionState
{
public sealed class MySessionState : IHttpSessionState
{
const int MAX_TIMEOUT = 24 * 60; // Timeout cannot exceed 24 hours.
string pId;
ISessionStateItemCollection pSessionItems;
HttpStaticObjectsCollection pStaticObjects;
int pTimeout;
bool pNewSession;
HttpCookieMode pCookieMode;
SessionStateMode pMode;
bool pAbandon;
bool pIsReadonly;
public MySessionState(string id,
ISessionStateItemCollection sessionItems,
HttpStaticObjectsCollection staticObjects,
int timeout,
bool newSession,
HttpCookieMode cookieMode,
SessionStateMode mode,
bool isReadonly)
{
pId = id;
pSessionItems = sessionItems;
pStaticObjects = staticObjects;
pTimeout = timeout;
pNewSession = newSession;
pCookieMode = cookieMode;
pMode = mode;
pIsReadonly = isReadonly;
}
public int Timeout
{
get { return pTimeout; }
set
{
if (value <= 0)
throw new ArgumentException("Timeout value must be greater than zero.");
if (value > MAX_TIMEOUT)
throw new ArgumentException("Timout cannot be greater than " + MAX_TIMEOUT.ToString());
pTimeout = value;
}
}
public string SessionID
{
get { return pId; }
}
public bool IsNewSession
{
get { return pNewSession; }
}
public SessionStateMode Mode
{
get { return pMode; }
}
public bool IsCookieless
{
get { return CookieMode == HttpCookieMode.UseUri; }
}
public HttpCookieMode CookieMode
{
get { return pCookieMode; }
}
//
// Abandon marks the session as abandoned. The IsAbandoned property is used by the
// session state module to perform the abandon work during the ReleaseRequestState event.
//
public void Abandon()
{
pAbandon = true;
}
public bool IsAbandoned
{
get { return pAbandon; }
}
//
// Session.LCID exists only to support legacy ASP compatibility. ASP.NET developers should use
// Page.LCID instead.
//
public int LCID
{
get { return Thread.CurrentThread.CurrentCulture.LCID; }
set { Thread.CurrentThread.CurrentCulture = CultureInfo.ReadOnly(new CultureInfo(value)); }
}
//
// Session.CodePage exists only to support legacy ASP compatibility. ASP.NET developers should use
// Response.ContentEncoding instead.
//
public int CodePage
{
get
{
if (HttpContext.Current != null)
return HttpContext.Current.Response.ContentEncoding.CodePage;
else
return Encoding.Default.CodePage;
}
set
{
if (HttpContext.Current != null)
HttpContext.Current.Response.ContentEncoding = Encoding.GetEncoding(value);
}
}
public HttpStaticObjectsCollection StaticObjects
{
get { return pStaticObjects; }
}
public object this[string name]
{
get { return pSessionItems[name]; }
set { pSessionItems[name] = value; }
}
public object this[int index]
{
get { return pSessionItems[index]; }
set { pSessionItems[index] = value; }
}
public void Add(string name, object value)
{
pSessionItems[name] = value;
}
public void Remove(string name)
{
pSessionItems.Remove(name);
}
public void RemoveAt(int index)
{
pSessionItems.RemoveAt(index);
}
public void Clear()
{
pSessionItems.Clear();
}
public void RemoveAll()
{
Clear();
}
public int Count
{
get { return pSessionItems.Count; }
}
public NameObjectCollectionBase.KeysCollection Keys
{
get { return pSessionItems.Keys; }
}
public IEnumerator GetEnumerator()
{
return pSessionItems.GetEnumerator();
}
public void CopyTo(Array items, int index)
{
foreach (object o in items)
items.SetValue(o, index++);
}
public object SyncRoot
{
get { return this; }
}
public bool IsReadOnly
{
get { return pIsReadonly; }
}
public bool IsSynchronized
{
get { return false; }
}
}
}
Imports System.Web
Imports System.Web.SessionState
Imports System.Collections
Imports System.Collections.Specialized
Imports System.Text
Imports System.Threading
Imports System.Globalization
Namespace Samples.AspNet.SessionState
Public NotInheritable Class MySessionState
Implements IHttpSessionState
Const MAX_TIMEOUT As Integer = 24 * 60 ' Timeout cannot exceed 24 hours.
Dim pId As String
Dim pSessionItems As ISessionStateItemCollection
Dim pStaticObjects As HttpStaticObjectsCollection
Dim pTimeout As Integer
Dim pNewSession As Boolean
Dim pCookieMode As HttpCookieMode
Dim pMode As SessionStateMode
Dim pAbandon As Boolean
Dim pIsReadonly As Boolean
Public Sub New(id As String, _
sessionItems As ISessionStateItemCollection, _
staticObjects As HttpStaticObjectsCollection, _
timeout As Integer, _
newSession As Boolean, _
cookieMode As HttpCookieMode, _
mode As SessionStateMode, _
isReadonly As Boolean)
pId = id
pSessionItems = sessionItems
pStaticObjects = staticObjects
pTimeout = timeout
pNewSession = newSession
pCookieMode = cookieMode
pMode = mode
pIsReadonly = isReadonly
End Sub
Public Property Timeout As Integer Implements IHttpSessionState.Timeout
Get
Return pTimeout
End Get
Set
If value <= 0 Then _
Throw New ArgumentException("Timeout value must be greater than zero.")
If value > MAX_TIMEOUT Then _
Throw New ArgumentException("Timout cannot be greater than " & MAX_TIMEOUT.ToString())
pTimeout = value
End Set
End Property
Public ReadOnly Property SessionID As String Implements IHttpSessionState.SessionID
Get
Return pId
End Get
End Property
Public ReadOnly Property IsNewSession As Boolean Implements IHttpSessionState.IsNewSession
Get
Return pNewSession
End Get
End Property
Public ReadOnly Property Mode As SessionStateMode Implements IHttpSessionState.Mode
Get
Return pMode
End Get
End Property
Public ReadOnly Property IsCookieless As Boolean Implements IHttpSessionState.IsCookieLess
Get
Return CookieMode = HttpCookieMode.UseUri
End Get
End Property
Public ReadOnly Property CookieMode As HttpCookieMode Implements IHttpSessionState.CookieMode
Get
Return pCookieMode
End Get
End Property
'
' Abandon marks the session as abandoned. The IsAbandoned property is used by the
' session state module to perform the abandon work during the ReleaseRequestState event.
'
Public Sub Abandon() Implements IHttpSessionState.Abandon
pAbandon = True
End Sub
Public ReadOnly Property IsAbandoned As Boolean
Get
Return pAbandon
End Get
End Property
'
' Session.LCID exists only to support legacy ASP compatibility. ASP.NET developers should use
' Page.LCID instead.
'
Public Property LCID As Integer Implements IHttpSessionState.LCID
Get
Return Thread.CurrentThread.CurrentCulture.LCID
End Get
Set
Thread.CurrentThread.CurrentCulture = CultureInfo.ReadOnly(new CultureInfo(value))
End Set
End Property
'
' Session.CodePage exists only to support legacy ASP compatibility. ASP.NET developers should use
' Response.ContentEncoding instead.
'
Public Property CodePage As Integer Implements IHttpSessionState.CodePage
Get
If Not HttpContext.Current Is Nothing Then
Return HttpContext.Current.Response.ContentEncoding.CodePage
Else
Return Encoding.Default.CodePage
End If
End Get
Set
If Not HttpContext.Current Is Nothing Then _
HttpContext.Current.Response.ContentEncoding = Encoding.GetEncoding(value)
End Set
End Property
Public ReadOnly Property StaticObjects As HttpStaticObjectsCollection _
Implements IHttpSessionState.StaticObjects
Get
Return pStaticObjects
End Get
End Property
Public Property Item(name As String) As Object Implements IHttpSessionState.Item
Get
Return pSessionItems(name)
End Get
Set
pSessionItems(name) = value
End Set
End Property
Public Property Item(index As Integer) As Object Implements IHttpSessionState.Item
Get
Return pSessionItems(index)
End Get
Set
pSessionItems(index) = value
End Set
End Property
Public Sub Add(name As String, value As Object) Implements IHttpSessionState.Add
pSessionItems(name) = value
End Sub
Public Sub Remove(name As String) Implements IHttpSessionState.Remove
pSessionItems.Remove(name)
End Sub
Public Sub RemoveAt(index As Integer) Implements IHttpSessionState.RemoveAt
pSessionItems.RemoveAt(index)
End Sub
Public Sub Clear() Implements IHttpSessionState.Clear
pSessionItems.Clear()
End Sub
Public Sub RemoveAll() Implements IHttpSessionState.RemoveAll
Clear()
End Sub
Public ReadOnly Property Count As Integer Implements IHttpSessionState.Count
Get
Return pSessionItems.Count
End Get
End Property
Public ReadOnly Property Keys As NameObjectCollectionBase.KeysCollection _
Implements IHttpSessionState.Keys
Get
Return pSessionItems.Keys
End Get
End Property
Public Function GetEnumerator() As IEnumerator Implements IHttpSessionState.GetEnumerator
Return pSessionItems.GetEnumerator()
End Function
Public Sub CopyTo(items As Array, index As Integer) Implements IHttpSessionState.CopyTo
For Each o As Object In items
items.SetValue(o, index)
index += 1
Next
End Sub
Public ReadOnly Property SyncRoot As Object Implements IHttpSessionState.SyncRoot
Get
Return Me
End Get
End Property
Public ReadOnly Property IsReadOnly As Boolean Implements IHttpSessionState.IsReadOnly
Get
Return pIsReadonly
End Get
End Property
Public ReadOnly Property IsSynchronized As Boolean Implements IHttpSessionState.IsSynchronized
Get
Return False
End Get
End Property
End Class
End Namespace
注解
会话状态容器提供对当前会话的会话状态值和相关信息的访问。 会话状态容器中包含的会话信息使用 Session 属性通过 HttpSessionState 类向应用程序代码公开。 类 HttpSessionState 是会话状态容器的包装类。
会话状态容器的 ASP.NET 实现是 HttpSessionStateContainer 类。 在请求开始时,在 AcquireRequestState 事件期间, SessionStateModule 创建并填充对象 HttpSessionStateContainer ,并将其分配给当前 HttpContext。 在请求结束时,在ReleaseRequestState事件期间,SessionStateModule从当前HttpContext检索HttpSessionStateContainer对象并执行任何所需的会话工作,例如将会话值写入会话存储或放弃会话。 如果请求突然终止(例如通过重定向),则 SessionStateModule 通过调用 EndRequest 方法执行相同的清理。
若要创建自定义会话状态容器,请创建实现 接口的 IHttpSessionState 类。 如果要创建自己的自定义会话状态容器,还必须将 替换为 SessionStateModule 自己的自定义模块。 自定义模块将创建自定义会话状态容器的实例,并使用 方法将其添加到当前HttpContextAddHttpSessionStateToContext。 类概述中包含 SessionStateUtility 自定义会话状态模块的示例。
属性
CodePage |
获取或设置当前会话的代码页标识符。 |
CookieMode |
获取一个值,该值指示是否为无 Cookie 会话配置应用程序。 |
Count |
获取会话状态项集合中的项数。 |
IsCookieless |
获取一个值,该值指示会话 ID 是嵌入在 URL 中还是存储在 HTTP Cookie 中。 |
IsNewSession |
获取一个值,该值指示会话是否是与当前请求一起创建的。 |
IsReadOnly |
获取一个值,该值指示会话是否为只读。 |
IsSynchronized |
获取一个值,该值指示对会话状态值的集合的访问是否是同步(线程安全)的。 |
Item[Int32] |
按数字索引获取或设置会话状态项的值。 |
Item[String] |
按名称获取或设置会话状态项值。 |
Keys |
获取存储在会话状态项集合中的所有值的键的集合。 |
LCID |
获取或设置当前会话的区域设置标识符 (LCID)。 |
Mode |
获取当前会话状态模式。 |
SessionID |
获取会话的唯一会话标识符。 |
StaticObjects |
获取由 ASP.NET 应用程序文件 Global.asax 中的 |
SyncRoot |
获取一个对象,可使用该对象同步会话状态值的集合的访问权限。 |
Timeout |
获取或设置在会话状态提供程序终止会话之前各请求之间所允许的超时期限(以分钟为单位)。 |
方法
Abandon() |
结束当前会话。 |
Add(String, Object) |
向会话状态集合添加一个新项。 |
Clear() |
从会话状态项集合中清除所有值。 |
CopyTo(Array, Int32) |
将会话状态项值的集合复制到一维数组中(从数组的指定索引处开始)。 |
GetEnumerator() |
返回一个枚举数,可用来读取当前会话中所有会话状态项值。 |
Remove(String) |
删除会话状态项集合中的某个项。 |
RemoveAll() |
从会话状态项集合中清除所有值。 |
RemoveAt(Int32) |
删除会话状态项集合中指定索引处的项。 |