日期:2013-07-08  浏览次数:21231 次

[前言:]ASP.NET是微软提供的最新的开发基于Web的应用程序的技术。它提供了大量的比传统ASP脚本技术的好处,包括:

  1)通过把UI表现层(presentation)与商业逻辑(business logic)分开建立了更好的开发结构;

  2)使用完全编译的代码代替了传统ASP的代码翻译;

  3)它编译特性与每个支持的方法协同,这意味着使用ASP.NET的站点比使用传统的ASP站点的性能更高。

  尽管把存在的ASP应用程序转换到ASP.NET有很多潜在的好处,也有些ASP应用程序任务很重要并且复杂。转换过程可能需要更多资源并给应用程序带来更多风险。解决这些问题的途径之一是同时运行ASP和ASP.NET应用程序,在一个时刻将一个对话转换为ASP.NET。为了同时运行新旧程序,需要建立一个机制在传统的ASP与ASP.NET间共享对话状态。本文讨论的是怎样使用.NET框架组件的类和序列化特性共享状态信息。

  概述

  Cookie是Web应用程序识别用户对话的最常用的方法,可以用于识别传统的ASP和ASP.NET对话状态。在ASP脚本中状态信息保存在内存中,不能与其它应用程序(例如ASP.NET)共享。如果对话状态使用通用格式保存在微软SQL Server中,它就可以被传统的ASP和ASP.NET共同访问。

  在本例中,名为mySession的Cookie用于识别用户对话。当用户对Web应用程序作出请求时,将为该用户产生唯一的Cookie用于识别对话。在随后的请求中,浏览器将该唯一的Cookie发送回服务器用来识别对话。在被请求的Web页载入前,一个自定义对象将使用该唯一的Cookie从SQL Server中重新载入用户对话数据。通过自定义对象Web页中的对话状态是可以访问的。Web请求完成后,如果请求终止,对话数据将保存回SQL Server(见图1)。

图1.数据流简单图

  ASP.NET实现

  在ASP.NET中每一个Web页都衍生自System.Web.UI.Page类。Page类集合了HttpSession对象的一个实例用于处理对话数据。在本例中,叫做SessionPage的自定义Page类来衍生自System.Web.UI.Page,提供了类似Page类的所有特性。唯一的区别是默认的HttpSession使用自定义的对话对象重载了(对实例变量使用new修改符,C#允许衍生的类隐藏基类的成员)。


public class SessionPage : System.Web.UI.Page
{
 ...
 public new mySession Session = null;
 ...
}

  自定义的对话类使用HybridDictionary对象来相应保存内存中的对话状态(HybridDictionary可用于处理任意数量的对话元素)。为了与传统的ASP通用,该自定义对话对象将限制对话数据类型为字符串型(默认的HttpSession允许对话保存任意类型的数据,不能与传统的ASP通用)。


[Serializable]
public class mySession
{
 private HybridDictionary dic = new HybridDictionary();

 public mySession()
 {
 }

 public string this [string name]
 {
  get
  {
   return (string)dic[name.ToLower()];
  }
  set
  {
   dic[name.ToLower()] = value;
  }
 }
}


  Page类为定制暴露了不同的事件和方法。特别是OnInit方法用于设置Page对象的初始化状态。如果请求不包含名为mySession的Cookie,将为请求者产生一个新的mySession Cookie。另外,对话数据将使用自定义数据访问对象SessionPersistence从SQL Server中检索出来。DSN和SessionExpiration的值从web.config中检索。


override protected void OnInit(EventArgs e)
{
 InitializeComponent();
 base.OnInit(e);
}
private void InitializeComponent()
{
 cookie = this.Request.Cookies[sessionPersistence.SessionID];

 if (cookie == null)
 {
  Session = new mySession();
  CreateNewSessionCookie();
  IsNewSession = true;
 }
 else
  Session = sessionPersistence.LoadSession(
    Server.UrlDecode(cookie.Value).ToLower().Trim(),
    dsn,
    SessionExpiration
   );

  this.Unload += new EventHandler(this.PersistSession);
}
private void CreateNewSessionCookie()
{
 cookie = new HttpCookie(sessionPersistence.SessionID,
 sessionPersistence.GenerateKey());
 this.Response.Cookies.Add(cookie);
}


  SessionPersistence类使用微软.NET框架组件的BinaryFormatter来串行化和并行化对话状态为二进制格式以提供最佳性能。结果的二进制对话数据接着作为图象字段类型被存入SQL Server。


public mySession LoadSession(string key, string dsn,
int SessionExpiration)
{
 SqlConnection conn = new SqlConnection(dsn);
 SqlCommand LoadCmd = new SqlCommand();
 LoadCmd.CommandText = command;
 LoadCmd.Connection = conn;
 SqlDataReader reader = null;
 mySession Session = null;

 try
 {
  LoadCmd.Parameters.Add("@ID", new Guid(key));
  conn.Open();
  reader = LoadCmd.ExecuteReader();
  if (reader.Read())
  {
   DateTime LastAccessed =reader.GetDateTime(1).AddMinutes(SessionExpiration);
   if (LastAccessed >= DateTime.Now)
    Session = Deserialize((Byte[])reader["Data"]);
  }
 }
 finally
 {
  if (reader != null)
   reader.Close();
  if (conn != null)
   conn.Close();
 }

 return Session;
}
private mySession Deserialize(Byte[] state)
{
 if (state == null) return null;

 mySession Session = null;
 Stream stream = null;

 try
 {
  stream = new MemoryStream();