StackOverflow异常

时间:2011-04-09 12:07:54

标签: c# asp.net stack-overflow

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

在page_load事件中,我正在调用

       if (mySession.Current._isCustomer)
       {

            Response.Redirect("Products.aspx");
       }

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

它给出了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;
        }

    }
}

3 个答案:

答案 0 :(得分:6)

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

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

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

即使使用单身mySession也无法解决问题。

执行此代码时:

session = new mySession();

它尝试初始化新的 ShoppingCard 。购物卡要求单身的mysession实例。这行代码尚未执行:

HttpContext.Current.Session["__MySession__"] = session;

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

这意味着堆栈溢出!

你可以这样纠正:

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

在代码中查看我的评论。

答案 1 :(得分:1)

问题来自于mySessionShoppingCart之间的关系。

mySession的成员变量定义如下:

public ShoppingCart _cart = new ShoppingCart();

当调用mySession的构造函数时,将实例化ShoppingCart的实例。当ShoppingCart的构造函数执行时,它会调用mySession.Current静态属性。因为ShoppingCart的构造函数是从同一个属性中调用的(请记住,我们仍在原始静态调用中创建mySession的实例),它继续以这种方式递归,直到{{1}提出来了。

要解决此问题,建议您查看一下StackOverflowException课程。首先,为什么它需要一个自身实例作为成员变量?其次,如果ShoppingCart需要知道有关ShoppingCart内容的信息,则您的封装不正确。我建议您将所需信息传递给mySession的构造函数,以避免回拨ShoppingCart

答案 2 :(得分:0)

鉴于您的更新问题,我认为如果您正确遵循.Net命名准则(如我对您的问题的评论中所述),您应该能够轻松找出问题所在。我怀疑你的呼叫代码是相似的,并且不遵循指南会模糊你认为正在发生的事情。

作为第一步,我建议进行清理;它可能会清楚地说明你造成溢出的地方。