StackOverflow异常

use*_*636 1 c# asp.net stack-overflow

mscorlib.dll中发生了未处理的"System.StackOverflowException"类型异常

在我调用的page_load事件中

       if (mySession.Current._isCustomer)
       {

            Response.Redirect("Products.aspx");
       }
Run Code Online (Sandbox Code Playgroud)

mySession类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ShoppingCartWebApp
{
    public class mySession
    {
            // private constructor
    private mySession() {}

// Gets the current session.
public static mySession Current
{
  get
  {
    mySession session =
      (mySession)HttpContext.Current.Session["__MySession__"];
    if (session == null)
    {
      session = new mySession();
      HttpContext.Current.Session["__MySession__"] = session;
    }
    return session;
  }
}

// **** add your session properties here, e.g like this:
public string _Property1 { get; set; }
public DateTime _date { get; set; }
public String _loginId { get; set; }

public string _firstName { get; set; }
public string _userName { get; set; }
public string _role { get; set; }
public Boolean _isCustomer = false;
public Boolean _isAuth = false;
public Boolean _isGuest = true;
public ShoppingCart _cart = new ShoppingCart();

public ShoppingCart instance
{
    get
    {

        return _cart;
    }

    set
    {
        _cart = value;
    }
}



public void abandonSession()
{
   // _date = 
        _loginId = null;
        _firstName = null;
        _cart = null;
        _userName = null;
        _role = null;
        _isCustomer = false;
        _isAuth = false;
    }
    }
}
Run Code Online (Sandbox Code Playgroud)

它给出了stackoverflow异常.为什么?

ShoppingCart类:

public class ShoppingCart
{
    #region ListCart

    public List<CartItem> Items { get; private set; }

    public static SqlConnection conn = new SqlConnection(connStr.connString);
    #endregion

    #region CartSession

    public ShoppingCart cart;

    public ShoppingCart() 
    {

        if (mySession.Current._cart == null)
        {
            cart = new ShoppingCart();
            cart.Items = new List<CartItem>();

            if (mySession.Current._isCustomer)
                cart.Items = ShoppingCart.loadCart(mySession.Current._loginId);

            mySession.Current._cart = cart;
        }
        else
        {
            cart = mySession.Current._cart;
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

Far*_*ker 6

这行代码导致无限循环和堆栈溢出:

if (mySession.Current._isCustomer)
                cart.Items = ShoppingCart.loadCart(mySession.Current._loginId);
Run Code Online (Sandbox Code Playgroud)

它由mysession类的每个实例初始化.并使用其父类.

即使使用单例mySession也无法解决问题.

当此代码执行时:

session = new mySession();
Run Code Online (Sandbox Code Playgroud)

它试图初始化新的ShoppingCard.购物卡要求单身的mysession实例.这行代码还没有执行:

HttpContext.Current.Session["__MySession__"] = session;
Run Code Online (Sandbox Code Playgroud)

所以去创建我的会话的新实例和...

这意味着堆栈溢出!

你可以像这样纠正它:

public static mySession Current
{
  get
  {
    mySession session =
      (mySession)HttpContext.Current.Session["__MySession__"];
    if (session == null)
    {
      session = new mySession();
      HttpContext.Current.Session["__MySession__"] = session;
      session._cart = new ShoppingCart(); //initialize your shoppoing car after adding variable to session !
    }
    return session;
  }
}

public ShoppingCart _cart;// = new ShoppingCart(); remove initialization
Run Code Online (Sandbox Code Playgroud)

看看我在代码中的评论.