很(hěn)簡單的東西,因為(wèi)在學(xué)習中(zhōng)遇到了,所以記錄下來.
事情的起因是,我在做一個購(gòu)物(wù)藍時,将一個自定義的類CartManager整個放進Session中(zhōng),它的部分(fēn)代碼如下,其實就是有(yǒu)一個Private的ArrayList成員_cart用(yòng)來放CartInfo類實例,而CartInfo類又(yòu)包括一個成員ProductInfo _product和一個double _moneny...并不複雜.但是我都沒有(yǒu)弄任何Serializable的東西,于是...
本機調試沒問題,放到服務(wù)器上卻發現這個購(gòu)物(wù)車(chē)表現非常怪異,時好時壞,總覺得好象Session裏的東西亂得很(hěn),有(yǒu)時能(néng)存進去有(yǒu)時存不進?
比較了本機與服務(wù)器的環境,我知道問題肯定與SessionState有(yǒu)關.因為(wèi)服務(wù)器用(yòng)了Web Farm(并且将最大工(gōng)作(zuò)進程數設置成了10).
一般我們在做一個WEB Application的時候,它的SessionState的Mode=InProc的,可(kě)參見web.config文(wén)件中(zhōng)的配置
<sessionState
mode="InProc"
stateConnectionString="tcpip=127.0.0.1:42424"
sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes"
cookieless="false"
timeout="20"
/>
在服務(wù)器上,因為(wèi)存在多(duō)個工(gōng)作(zuò)進程,所以需要将它的寫法改成 mode=StateServer了,否則就會造成前面所說的Session中(zhōng)的值不确定的現象.但是,如果簡單地這樣改一下,系統又(yòu)報錯說對于以StateServer 或者 SqlServer兩種方式保存會話狀态,要求對象是可(kě)序列化的(大意如此)...所以我們還需要再将對象做一下可(kě)序列化聲明.
如果要保存的對象很(hěn)簡單,都是由基本類型組成的,就隻需要聲明一下屬性即可(kě),如:
[Serializable()]
public class ProductInfo {
private string f_SysID;
public string SysID {
get {
return this.f_SysID;
}
set {
this.f_SysID = value;
}
}
對于本例中(zhōng),CartInfo 與 ProductInfo兩個類,可(kě)以這樣聲明一下.隻是CartManager就稍多(duō)幾句話,如下:
[Serializable]
public class CartManager : ISerializable
{
private ArrayList _cart=new ArrayList();
public CartManager()
{
}
protected CartManager(SerializationInfo info, StreamingContext context)
{
this._cart=(ArrayList)info.Getvalue("_cart",typeof(ArrayList));
}
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
info.Addvalue("_cart",this._cart);
}
private CartInfo findCartInfo(string sid)
{
foreach(CartInfo ci in this._cart)
{
if( ci.Product.SysID.Equals(sid) ) return ci;
}
return null;
}
public ArrayList getCart()
{
return this._cart;
}
這樣實現了整個CartManager--CartInfo--ProductInfo的可(kě)序列化聲明,于是就一切正常了...
文(wén)章出自:
http://www.cnblogs.com/sharetop/archive/2005/10/08/250286.html