在ASP与ASP.NET之间共享对话状态[1]
[入库:2005年8月18日] [更新:2007年3月25日]
|
|
|
|
|
| |
|
[前言:]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(); 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); sqlcommand savecmd = new sqlcommand(); savecmd.connection = conn;
try { if (isnewsession) savecmd.commandtext = insertstatement; else savecmd.commandtext = updatestatement;
savecmd.parameters.add("@id", new guid(key)); savecmd.parameters.add("@data", serialize(session)); savecmd.parameters.add("@lastaccessed", datetime.now.tostring());
conn.open(); savecmd.executenonquery(); } finally { if (conn != null) conn.close(); } } private byte[] serialize(mysession session) { if (session == null) return null;
stream stream = null; byte[] state = null;
try { iformatter formatter = new binaryformatter(); stream = new memorystream(); formatter.serialize(stream, session); state = new byte[stream.length]; stream.position = 0; stream.read(state, 0, (int)stream.length); stream.close(); } finally { if (stream != null) stream.close(); } return state; }
sessionpage类以及与它相关的类被封装在sessionutility组件中。在一个新asp.net项目中,需要作sessionutility组件的引用,为了与传统的asp代码共享对话,每个页面由sessionpage代替page类衍生出来。一旦移植完成了,新应用程序能通过说明sessionpage类中定义的对话变量切换回使用原来的httpsession对象来显示基本的httpsession。 |
本文关键:在ASP与ASP.NET之间共享对话状态
本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)