2009-04-13 105 views
1

我有序列化我的會話對象的問題。我做錯了什麼?我試着用XmlSerializer和BinaryFormatter序列化這個對象,並沒有問題。會話狀態序列化

當我嘗試籃下對象保存到會話我會得到錯誤:

Unable to serialize the session state. In 'StateServer' and 'SQLServer' mode, ASP.NET will serialize the session state objects, and as a result non-serializable objects or MarshalByRef objects are not permitted. The same restriction applies if similar serialization is done by the custom session state store in 'Custom' mode.

這裏是對象:

[Serializable] 
public class Basket 
{ 
    #region Fields (2) 

    [NonSerialized] 
    private CMS.CmsEntity db; 

    private List<ShopOrderItem> ShopOrderItems; 

    #endregion Fields 

    #region Properties (2) 

    public bool IsEmpty 
    { 
     get 
     { 
      return (this.Items.Count == 0); 
     } 
    } 

    public List<ShopOrderItem> Items 
    { 
     get 
     { 
      if (this.ShopOrderItems == null) 
      { 
       this.ShopOrderItems = new List<ShopOrderItem>(); 
      } 

      return this.ShopOrderItems; 
     } 
     set 
     { 
      this.ShopOrderItems = value; 
     } 
    } 

    #endregion Properties 

    #region Delegates and Events (1) 

    // Events (1)  

    public event EventHandler CartItemsChanged; 

    #endregion Delegates and Events 

    #region Methods (9) 


    public int CountItems() 
    { 
     return this.ShopOrderItems.Sum(s => s.Quantity); 
    } 
    public decimal CountTotalAmount() 
    { 
     ... 
    } 
    public decimal CountTotalAmountWithoutVAT() 
    { 
     ... 
    } 
    public CMS.ProductVariant GetProductVariantById(int id) 
    { 
     ... 
    } 


    #region AddProductToCart 
    public void AddProductToCart(int productVariantId, int quantity) 
    { 
     AddProductToCart(GetProductVariantById(productVariantId), quantity); 
    } 
    public void AddProductToCart(ProductVariant productVariant, int quantity) 
    { 
     ... 
    } 
    #endregion 

    #region RemoveProductFromCart 
    public void RemoveProductFromCart(int productVariantId) 
    { 
     RemoveProductFromCart(GetProductVariantById(productVariantId)); 
    } 
    public void RemoveProductFromCart(ProductVariant productVariant) 
    { 
     .. 
    } 
    #endregion 

    #region UpdateProductQuantity 
    public void UpdateProductQuantity(int variantId, int quantity, bool isRelative) 
    { 
     UpdateProductQuantity(GetProductVariantById(variantId), quantity, isRelative); 
    } 
    public void UpdateProductQuantity(ProductVariant productVariant, int quantity, bool isRelative) 
    { 
     ... 
    } 
    #endregion 

    #endregion Methods} 

代碼與會議操縱:

public static class CurrentSession 
{     

#region public static Basket Basket 
public static Basket Basket 
    { 
     get 
     { 
         Basket c = SessionHelper.GetSessionObject("UserCart") as Basket; 

         if (c == null) 
         { 
          c = new Basket(); 
          SessionHelper.SetSessionObject("UserCart", c); // If i comment this line, exception is not thrown 
         } 

         return c; 
     } 
     set 
     { 
         SessionHelper.SetSessionObject("UserCart", value); 
     } 
    } 
    #endregion 

} 

如果我使用InProc會話狀態,它的工作原理。所以它必須在序列化過程中

回答

0

我發現的bug ..

序列化過程中可能不喜歡的活動: -/

我只使用InProc會話。

+0

您是否使用我的「二分查找」方法發現了這一點?如果你嘗試過,那麼你會發現這樣的問題。 – 2009-04-13 18:06:26

0

因爲我們沒有剩下的代碼,所以我們不知道該類的哪一部分是問題。但可以告訴。

評論一半的類,然後再試一次。如果它有效,那麼你註釋掉的一半就有問題了。如果它不起作用,那麼你沒有註釋掉的一半有問題。無論哪種方式,註釋出問題的一半的部分,並再次嘗試...

這就像一個二進制搜索。

+0

我添加了其餘的代碼,我只是將對象保存到會話Session [「basket」] = new Basket(); – 2009-04-13 13:42:06