Reputation: 3424
An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll
In page_load event i am calling
if (mySession.Current._isCustomer)
{
Response.Redirect("Products.aspx");
}
mySession class:
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;
}
}
}
it gives a stackoverflow exception. why?
ShoppingCart Class:
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;
}
}
}
Upvotes: 2
Views: 2544
Reputation: 82944
The problem comes because of the relationship between mySession
and ShoppingCart
.
mySession
has a member variable defined like so:
public ShoppingCart _cart = new ShoppingCart();
When the constructor of mySession
is called, an instance of ShoppingCart
is instantiated. When the constructor of ShoppingCart
executes, it calls the mySession.Current
static property. Because the constructor of ShoppingCart
was called from within this same property (remember, we are still creating an instance of mySession
in the original static call), it continues to recurse in this way until a StackOverflowException
is raised.
To fix this, I suggest you take a look at your ShoppingCart
class. Firstly, why does it need an instance of itself as a member variable? Secondly, if ShoppingCart
needs to know information about the contents of mySession
, your encapsulation is not correct. I suggest you pass the information needed into the constructor of ShoppingCart
to avoid making a call back to mySession.Current
.
Upvotes: 1
Reputation: 3606
This line of code causes infinite loop and stack overflow :
if (mySession.Current._isCustomer)
cart.Items = ShoppingCart.loadCart(mySession.Current._loginId);
it is initialized by each instance of mysession class. and its using its parent class.
even using singleton mySession can not solve the problem.
when this code is executing :
session = new mySession();
it tries to initialize new ShoppingCard. shopping card asks for singleton instance of mysession. this line of code is not executed yet :
HttpContext.Current.Session["__MySession__"] = session;
so goes to create a new instance of my session and ...
this means stack overflow !
you can correct it like this :
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
look at my comments in code.
Upvotes: 7
Reputation: 8562
Given your updated question, I think if you properly followed .Net naming guidelines (as outlined in my comment on your question), you should be able to easily figure out where the problem is. I suspect your calling code is similar, and not following the guidelines is obscuring what is actually happening from what you think is happening.
As a first step I would recommend doing this cleanup; it will likely make it clear where you're causing the overflow.
Upvotes: 0