关于session一些知识

1)对于值类型的变量,Session中保存的是值类型的拷贝
Session["__test0"] = 1;
int i = (int)Session["__test0"]+1;
int j = (int)Session["__test0"];
结果i=2,j=1
2)对于引用类新的变量,Session中保存的是引用
CDACommon cda  = new CDACommon();
Session["__test"]
= cda.GetDataSet("select top 1 * from tb_customer");
DataSet ds
= (DataSet)Session["__test"];
DataSet ds2
= (DataSet)Session["__test"];
ds.Tables[
0].Rows[0][0]="9999";    
结果ds.Tables[0].Rows[0][0]=="9999"       ds2.Tables[0].Rows[0][0]=="9999";    
3) 新的浏览器窗口启动后,开始一个新的Session,触发Global的Session_Start的调用,从第一个浏览器窗口打开的浏览器窗口不启动新 的Session。Session过期后,执行页面的提交也会触发Session_Start,等于是新的一个Session。
4)对于Web Service,每个方法的调用都会启动一个Session,可以用下面的方法来使多个调用在同一个Session里
CWSSyscfg cwsCfg = new CWSSyscfg();
cwsCfg.CookieContainer
= new System.Net.CookieContainer();
CWSSyscfg 是一个Web Service类,Web Service的给代理类设置CookieContainer属性,只要多个代理的CookieContainer属性是相同的值,则对这些Web Service的调用在同一个Session。可以用单例模式来实现。
5)只要页面有提交活动,则Session的所有项都会保持,页面在20分钟(默认配置)内没有任何提交活动时Session会失效。Session内存储的多个数据项是整体失效的。
6)在session中如果保存的是非序列化的类比如DataView,在用SQLServer保存Session的模式下,无法使用。查看一个类是否是序列化的方法是,需看是否用[Serializable]来标记了该类。





如何获得当前Session中保存的所有对象

答:可以通过遍历所有的Session.Keys来获得。代码如下:

ArrayList sessionCollection = new ArrayList();

foreach (string strKey in Session.Keys){

sessionCollection.Add(Session[strKey]);

}

Session的超时设置是分钟还是秒?

答:是分钟,默认为20分钟。

如何将SortedList存储到Session或者Cache里?

答:请参考下面的方法:

SortedList x = new SortedList();

x.Add("Key1", "ValueA");

x.Add("Key2", "ValueB");

保存到Session中:

Session["SortedList1"] = x;

使用下面方法获得之:

SortedList y = (SortedList) Session["SortedList1"];

如何删除Session变量?

答:想要删除Session变量可以使用HttpSessionState.Remove()方法。

posted on 2007-07-30 15:24  Wenguan  阅读(286)  评论(0)    收藏  举报

导航