2009-04-13 8 views
1

Ich habe Probleme mit der Serialisierung meines Sitzungsobjekts. Was mache ich falsch? Ich habe versucht, das Objekt mit XmlSerializer und BinaryFormatter serialisieren und es gab kein Problem.Sitzungsstatusserialisierung

Wenn ich versuche, den Korb Objekt in die Sitzung speichern i Fehler bekommen:

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.

hier ist das Objekt:

[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, der mit Sitzung manipuliert:

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 

} 

Wenn ich InProc Session State verwenden, funktioniert es. Also muss es im Serialisierungsprozess sein

Antwort

0

fand ich den Fehler ..

Serialisierung Prozess wahrscheinlich Ereignisse nicht mag: -/

ich nur InProc Session verwenden.

+0

Haben Sie dies durch meine "binäre Suche" -Methode entdeckt? Wenn Sie es versucht hätten, hätten Sie dieses Problem auf diese Weise entdeckt. –

+0

Ich verwendete Methode Versuch-Fehler. :-) Sitzungsserialisierung mag Ereignisse einfach nicht. –

0

Da wir den Rest Ihres Codes nicht haben, können wir nicht sagen, welcher Teil dieser Klasse ein Problem ist. Aber Sie kann sagen.

Kommentieren Sie die Hälfte dieser Klasse aus und versuchen Sie es erneut. Wenn es funktioniert, hat die Hälfte, die du auskommentiert hast, das Problem darin. Wenn es nicht funktioniert, dann hat die Hälfte, die Sie nicht kommentiert haben, das Problem darin. Wie auch immer, kommentieren Sie die Hälfte des Teils mit dem Problem aus und versuchen Sie es erneut ...

Dies ist genau wie eine binäre Suche.

+0

Ich habe den Rest des Codes hinzugefügt, ich speichere das Objekt einfach in Session Session ["basket"] = new Basket(); –