views:

998

answers:

2

Hello, i have problem with serialization my session object. What i'm doing wrong? I tried serialize that object with XmlSerializer and BinaryFormatter and there was no problem.

When i try save the basket object to the session i'll get error:

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.

here is the object:

[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}

Code that manipulates with session:

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

}

if i use InProc Session State, it works. So it must be in serialization process

A: 

Since we don't have the rest of your code, we can't tell what part of that class is a problem. But you can tell.

Comment out half of that class and try again. If it works, then the half you commented out had the problem in it. If it doesn't work, then the half you did not comment out has the problem in it. Either way, comment out half of the part with the problem in it and try again...

This is just like a Binary Search.

John Saunders
i added rest of code, i simply save the object to session Session["basket"] = new Basket();
Jan Remunda
A: 

I found the bug..

Serialization process probably doesn't like events :-/

I have to use only InProc Session.

Jan Remunda
Did you discover this by using my "binary search" methodology? If you had tried it, then you would have discovered this problem that way.
John Saunders
I used method attempt-mistake. :-) Session serialization simply doesn't like events.
Jan Remunda