tring Name
{
get;
set;
}
///
/// 上下文ID
///
Guid UniqueID
{
get;
set;
}
///
/// 客户端IP
///
string ClientAddress
{
get;
set;
}
///
/// 应用所在物理路径
///
string ApplicationDirectory
{
get;
set;
}
///
/// 客户端请求的地址
///
string RequestURL
{
get;
set;
}
///
/// 客户端请求的上一URL
///
string ReferURL
{
get;
set;
}
///
/// 当前用户ID
///
string UserID
{
get;
set;
}
}
复制代码
第二:定义一个上下文句柄接口,用于获取和重置上下文。
public interface IContextHandler
{
///
/// 获取上下文
///
/// 返回当前上下文
IContext GetContext();
///
/// 设置当前上下文
///
void SetContext(IContext context);
}
复制代码
第三:定义上下文的通用实现类,基本思想就是在请求产生后,获取请求相关信息,将这些信息存储在HttpContext.Current.Items中。
View Code
public class CommonContextHandler : IContextHandler
{
private static readonly string _ContextDataName = "#CurrentContext#";
[ThreadStatic]
private static IContext _ContextInstance;
public CommonContextHandler()
{
}
#region IContextHandler Members
public IContext GetContext()
{
if (HttpContext.Current == null)
{
if (_ContextInstance == null)
{
_ContextInstance = CreateNewContext();
}
return _ContextInstance;
}
else
{
object obj = HttpContext.Current.Items[_ContextDataName];
if (obj == null)
{
HttpContext.Current.Items[_ContextDataName] = CreateNewContext();
obj = HttpContext.Current.Items[_ContextDataName];
}
return (IContext)obj;
}
}
public void SetContext(IContext context)
{
if (HttpContext.Current == null)
{
_ContextInstance = context;
}
else
{
object obj = HttpContext.Current.Items[_ContextDataName];
if (obj == null)
{
HttpContext.Current.Items[_ContextDataName] = context;
}
else
{
HttpContext.Current.Items[_ContextDataName] = context;
}
}
}
#endregion
#region 保护方法
protected virtual IContext CreateNewContext()
{
return new CommonContext();
}
#endregion
}
复制代码
第四:编写一个适用于web程序的上下文实体类,主要是为上下文实体类填充数据,以借记录数据时使用。
View Code
[Serializable]
public class SimpleWebContext : IContext
{
#region 私有成员
private Guid _UniqueID;
private string _Re |