我们在进行Web开发时经常会用到Session,用它去标识不同的会话,那么涉及到跨站点的时候如何实现Session共享呢?
通常的解决方案有:使用数据库、使用Cookies做中间桥等等。
下面介绍一种基于ASP.NET 2.0的,通过序列化和反序列化机制实现的一种解决方案。
首先看一下通常的服务器集群的网络拓扑结构:
利用序列化机制实现Session共享的原理:
1、Web Server 1的应用程序序列化Session信息为文本值(可以是Binary或Soap格式)
2、将序列化后的值写入文件,保存到File Server上
3、Web Server 2 对保存在File Server上的Session序列化后的值进行反序列化
4、在Web Server 2上重新构建Session值
下面我们来详细看看实现代码,分以下几个步骤:
1、创建一个类库工程:ShareSession
引入以下的命名空间:
System.configuration
System.Runtime.Serialization.Formatters.Soap
System.Web
2、创建一个类:SessionEntity
代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ShareSession
...{
/**//* *****************************
*
* Author : Xiaojun Liu
* Create Date : 2007.11.09
* Description :
*
* *****************************
*/
[Serializable]
public class SessionEntity
...{
[NonSerialized]
public string AUTH_GUID = "";
private Hashtable _HtShareSession;
public Hashtable HtShareSession
...{
get
...{
return this._HtShareSession;
}
set
...{
this._HtShareSession = value;
}
}
public SessionEntity(string guid)
...{
this.AUTH_GUID = guid;
}
}//
}//
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ShareSession
...{
/**//* *****************************
*
* Author : Xiaojun Liu
* Create Date : 2007.11.09
* Description :
*
* *****************************
*/
[Serializable]
public class SessionEntity
...{
[NonSerialized]
public string AUTH_GUID = "";
private Hashtable _HtShareSession;
public Hashtable HtShareSession
...{
get
...{
return this._HtShareSession;
}
set
...{
this._HtShareSession = value;
}
}
public SessionEntity(string guid)
...{
this.AUTH_GUID = guid;
}
}//
}//
3、创建一个序列化、反序列化操作的类:ShareSessionFormatter
代码如下:
using System;
using System.Web;
using System.Data;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization.Formatters.Soap;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections;
using System.IO;
namespace ShareSession
...{
/**//* *****************************
*
* Author : Xiaojun Liu
* Create Date : 2007.11.09
* Description :
*
* *****************************
*/
/**//// <summary>
/// 格式化操作器
/// </summary>
public class ShareSessionFormatter
...{
private static string _ShareSessionPath = System.Configuration.ConfigurationManager.AppSettings["ShareSessionPath"].ToString();
/**//// <summary>
/// 序列化格式器类型
/// </summary>
public enum FormatterType
...{
Binary,
Soap
};
/**//// <summary>
/// 执行序列化
/// </summary>
/// <param name="formatterType">类型</param>
public static void Serialize(FormatterType formatterType)
...{
if (HttpContext.Current.Session["AUTH_GUID"] == null)
...{
HttpContext.Current.Session["AUTH_GUID"] = Guid.NewGuid().ToString();
}
Hashtable ht = new Hashtable();
//遍历Session
foreach (string key in HttpContext.Current.Session.Contents.Keys)
...{
ht.Add(key, HttpContext.Current.Session[key]);
}
/**/////执行序列化
switch (formatterType)
...{
case FormatterType.Binary:
BinarySerialize(ht);
break;
case FormatterType.Soap:
SoapSerialize(ht);
break;
default:
break;
}
}
/**//// <summary>
/// 按照Binary格式序列化
/// </summary>
/// <param name="ht"></param>
private static void BinarySerialize(Hashtable ht)
...{
string guid = HttpContext.Current.Session["AUTH_GUID"].ToString();
SessionEntity obj = new SessionEntity(guid);
obj.HtShareSession = ht;
// 创建一个文件guid.xml
Stream stream = File.Open(_ShareSessionPath + guid + ".xml", FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
//执行序列化
formatter.Serialize(stream, obj);
stream.Close();
// 将对象置空
obj = null;
}
/**//// <summary>
/// 按照Soap格式序列化
/// </summary>
/// <param name="ht"></param>
private static void SoapSerialize(Hashtable ht)
...{
string guid = HttpContext.Current.Session["AUTH_GUID"].ToString();
SessionEntity obj = new SessionEntity(guid);
obj.HtShareSession = ht;
// 创建一个文件guid.xml
Stream stream = File.Open(_ShareSessionPath + guid + ".xml", FileMode.Create);
SoapFormatter formatter = new SoapFormatter();
//执行序列化
formatter.Serialize(stream, obj);
stream.Close();
// 将对象置空
obj = null;
}
/**//// <summary>
/// 反序列化
/// </summary>
/// <param name="formatterType">传送端的Session["AUTH_GUID"]</param>
/// <param name="guid"></param>
public static void Deserialize(FormatterType formatterType, string guid)
...{
switch (formatterType)
using System.Web;
using System.Data;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization.Formatters.Soap;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections;
using System.IO;
namespace ShareSession
...{
/**//* *****************************
*
* Author : Xiaojun Liu
* Create Date : 2007.11.09
* Description :
*
* *****************************
*/
/**//// <summary>
/// 格式化操作器
/// </summary>
public class ShareSessionFormatter
...{
private static string _ShareSessionPath = System.Configuration.ConfigurationManager.AppSettings["ShareSessionPath"].ToString();
/**//// <summary>
/// 序列化格式器类型
/// </summary>
public enum FormatterType
...{
Binary,
Soap
};
/**//// <summary>
/// 执行序列化
/// </summary>
/// <param name="formatterType">类型</param>
public static void Serialize(FormatterType formatterType)
...{
if (HttpContext.Current.Session["AUTH_GUID"] == null)
...{
HttpContext.Current.Session["AUTH_GUID"] = Guid.NewGuid().ToString();
}
Hashtable ht = new Hashtable();
//遍历Session
foreach (string key in HttpContext.Current.Session.Contents.Keys)
...{
ht.Add(key, HttpContext.Current.Session[key]);
}
/**/////执行序列化
switch (formatterType)
...{
case FormatterType.Binary:
BinarySerialize(ht);
break;
case FormatterType.Soap:
SoapSerialize(ht);
break;
default:
break;
}
}
/**//// <summary>
/// 按照Binary格式序列化
/// </summary>
/// <param name="ht"></param>
private static void BinarySerialize(Hashtable ht)
...{
string guid = HttpContext.Current.Session["AUTH_GUID"].ToString();
SessionEntity obj = new SessionEntity(guid);
obj.HtShareSession = ht;
// 创建一个文件guid.xml
Stream stream = File.Open(_ShareSessionPath + guid + ".xml", FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
//执行序列化
formatter.Serialize(stream, obj);
stream.Close();
// 将对象置空
obj = null;
}
/**//// <summary>
/// 按照Soap格式序列化
/// </summary>
/// <param name="ht"></param>
private static void SoapSerialize(Hashtable ht)
...{
string guid = HttpContext.Current.Session["AUTH_GUID"].ToString();
SessionEntity obj = new SessionEntity(guid);
obj.HtShareSession = ht;
// 创建一个文件guid.xml
Stream stream = File.Open(_ShareSessionPath + guid + ".xml", FileMode.Create);
SoapFormatter formatter = new SoapFormatter();
//执行序列化
formatter.Serialize(stream, obj);
stream.Close();
// 将对象置空
obj = null;
}
/**//// <summary>
/// 反序列化
/// </summary>
/// <param name="formatterType">传送端的Session["AUTH_GUID"]</param>
/// <param name="guid"></param>
public static void Deserialize(FormatterType formatterType, string guid)
...{
switch (formatterType)