2009-04-13 104 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