会话状态序列化 [英] Session state serialization

查看:46
本文介绍了会话状态序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的会话对象序列化有问题.我在做什么错?我尝试使用 XmlSerializer 和 BinaryFormatter 序列化该对象,但没有问题.

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:

无法序列化会话状态.在StateServer"和SQLServer"模式下,ASP.NET 将序列化会话状态对象,因此不允许使用不可序列化的对象或 MarshalByRef 对象.如果自定义会话状态存储在自定义"模式下完成类似的序列化,则同样的限制适用.

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 会话状态,它就可以工作.所以一定是在序列化过程中

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

推荐答案

我发现了错误..

序列化过程可能不喜欢事件:-/

Serialization process probably doesn't like events :-/

我必须只使用 InProc Session.

I have to use only InProc Session.

这篇关于会话状态序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆