加入收藏
联系我们
关于我们
 您现在的位置: 亿聪 >> 网络学院 >> 网络编程 >> ASP >> 正文  
  在ASP与ASP.NET之间共享对话状态         ★★★
在ASP与ASP.NET之间共享对话状态
[ 作者:    转贴自:开发者俱乐部    点击数:53237    更新时间:2003/8/29    文章录入:亿聪 ]

 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();
  stream.Write(state, 0, state.Length);
  stream.Position = 0;
  IFormatter formatter = new BinaryFormatter();
  Session = (mySession)formatter.Deserialize(stream);
 }
 finally
 {
  if (stream != null)
   stream.Close();
 }
 return Session;
}


  在请求的末尾,Page类的Unload事件被启动了,一个同Unload事件一起注册的事件处理方法将串行化对话数据为二进制格式并将结果二进制数据存入SQL Server。


private void PersistSession(Object obj, System.EventArgs arg)
{ sessionPersistence.SaveSession(
  Server.UrlDecode(cookie.Value).ToLower().Trim(), dsn, Session, IsNewSession);
}
public void SaveSession(string key, string dsn,
mySession Session, bool IsNewSession)
{
 SqlConnection conn = new SqlConnection(dsn);

上一页  [1] [2] [3] [4] [5]  下一页

  • 上一篇文章: ASP 3.0高级编程(四十六)

  • 下一篇文章: ASP中常用的文件处理函数
  • 发表评论】【告诉好友】【打印此文】【关闭窗口
     最新5篇热点文章
  • Knoppix 4.0.2 免硬盘免安…[15146]

  • 通过ASP记录进行分页[19328]

  • ASP开发准则[11282]

  • ASP组件指南[11253]

  • ASP指南[11266]

  •  
     最新5篇推荐文章
  • Knoppix 4.0.2 免硬盘免安…[15146]

  • 如何让Win 2003系统更加安…[12513]

  • Nero超刻简明教程[33954]

  • PS商业实战-来杯茶,行吗?…[7090]

  • ASP深度揭密(下)[12236]

  •  
     相 关 文 章
    没有相关文章

      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
        没有任何评论
    设为首页 | 加入收藏 | 关于我们 | 联系我们 | 友情链接 | 版权声明 | 管理登录
    Copyright © 2000-2022 Yicong.com.All Rights Reserved.
    亿聪 版权所有 E-mail: