gpt4 book ai didi

c# - StackOverflow 异常

转载 作者:太空宇宙 更新时间:2023-11-03 18:44:34 25 4
gpt4 key购买 nike

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

在我调用的 page_load 事件中

       if (mySession.Current._isCustomer)
{

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

我的 session 类:

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

}
}

最佳答案

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

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

它由 mysession 类的每个实例初始化。及其父类。

即使使用单例mySession也不能解决问题。

当这段代码执行时:

session = new mySession();

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

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

因此创建我的 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

看看我在代码中的注释。

关于c# - StackOverflow 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5604936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com