Session State Serialization

I have a problem serializing my session object. What am I doing wrong? I tried serializing this object using XmlSerializer and BinaryFormatter, and there were no problems.

When I try to save the cart object to the session, I get an error:

Unable to serialize session state. In StateServer and SQLServer mode, ASP.NET will serialize session state objects, and as a result, non-serialization objects or MarshalByRef objects are not allowed. The same restriction applies if such serialization is performed in the user session state store in the "User" 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 the 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. Therefore, it must be in the process of serialization

+1
source share
2 answers

I found a mistake.

The serialization process probably doesn't like events: - /

I need to use only the InProc session.

0
source

Since we do not have the rest of your code, we cannot say which part of this class is the problem. But you can tell.

Comment on half of this class and try again. If this works, then in half you commented out the problem. If this does not work, then half that you have not commented on has a problem. In any case, comment on half of the problem with it and try again ...

This is like a binary search.

0
source

Source: https://habr.com/ru/post/917664/


All Articles