2011-04-09 79 views
1

类型 'System.StackOverflowException' 未处理的异常出现在mscorlib.dllStackOverflow的异常

在Page_Load事件我打电话

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例外。为什么?

的购物类别:

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

    } 
} 
+0

你如何使用这个单例类?正如我所看到的那样,没有任何问题 – 2011-04-09 12:12:31

+0

编辑忘记了包括 – user478636 2011-04-09 12:14:30

+2

哪一行出现异常?调用代码的样子是什么?另外,我强烈建议你遵循正常的.Net惯例;没有公共字段,大写类,属性和方法名称,不要使用以公共/受保护成员的下划线开头的名称(AbandonSession,FirstName等)。这将使C#人员更容易阅读您的代码。 – Andy 2011-04-09 12:15:10

回答

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 

看看我的代码中的注释。

+0

我在已编辑的问题中提供了代码.... 如果我评论说如果声明...一切正常 当前页面是Login.aspx – user478636 2011-04-09 12:19:36

+0

编辑。只需替换你的代码。每件事情都会好起来的。 – 2011-04-09 12:53:27

+0

谢谢.....它工作正常 – user478636 2011-04-09 13:03:48

0

鉴于您的更新问题,我认为如果您正确遵循.Net命名准则(如我对您问题的评论所述),您应该能够轻松找出问题所在。我怀疑你的调用代码是相似的,不遵循这些指导原则会掩盖你正在发生的事情正在发生的事情。

作为第一步,我会建议做这个清理;它可能会明确你在哪里引起溢出。

+1

命名约定不能引发stackoverflow异常! – 2011-04-09 12:18:21

+0

不,但他们可以使它很难遵循代码。我建议他按照惯例来帮助找出错误发生的位置。我敢打赌,当他打算进入一个领域时,他正在使用一个物业。 – Andy 2011-04-09 12:21:23

1

问题出在mySessionShoppingCart之间的关系。

mySession具有像这样限定的成员变量:

public ShoppingCart _cart = new ShoppingCart(); 

mySession的构造函数被调用,的ShoppingCart一个实例被实例化。当ShoppingCart的构造函数执行时,它会调用mySession.Current静态属性。由于ShoppingCart的构造函数是在同一属性中调用的(请记住,我们仍在原始静态调用中创建mySession的实例),它将继续以这种方式进行递归,直到StackOverflowException被引发。

要解决这个问题,我建议你看看你的ShoppingCart类。首先,它为什么需要一个自己的实例作为成员变量?其次,如果ShoppingCart需要知道有关mySession内容的信息,那么封装是不正确的。我建议您将所需的信息传递给ShoppingCart的构造函数,以避免拨打mySession.Current