Nugs
asked on
Make sure HttpContext.Current is valid and available
Morning,
I have a class that handles my sessions that have been created, i access this class to get or set the session. I do these so that i can easily mange all the session on my site from one class and then access the class to get the session values.
A typical property of the class would be something like this:
public int CustomerID
{
get
{
intCustomerID = Convert.ToInt32(HttpContex t.Current. Session["C ustomerID" ].ToString ());
return intCustomerID;
}
set
{
intCustomerID = value;
HttpContext.Current.Sessio n["Custome rID"] = intCustomerID.ToString();
}
}
This was producing some errors in my logs "Parameter is not valid" because it seems that the class trying to get the Session["CustomerID"] was using it to insert into the database but i suspect that HttpContext.Current was null or is not available. In this case i would in addition get the error "Object reference not set to an instance of an object." because Session["CustomerID"] could not be accessed...
this could sometime be the case with these sessions and sometimes HttpContex may not be available for the class calling this property. So i modified the method like so:
public int CustomerID
{
get
{
if (!Int32.TryParse(HttpConte xt.Current .Session[" CustomerID "].ToStrin g(), out intCustomerID))
{
intCustomerID = -1;
}
return intCustomerID;
}
set
{
intCustomerID = value;
HttpContext.Current.Sessio n["Custome rID"] = intCustomerID.ToString();
}
}
Just to try and catch it it was available or not... As you can see this does not work and i get the same results...
So question is how do i check if HttpContext.Current is available or not and if not return a -1 value... I can't check if the session is null cause HttpContext.Current.Sessio n is not available...
Hope that all made sense. Any ideas?
Nugs
I have a class that handles my sessions that have been created, i access this class to get or set the session. I do these so that i can easily mange all the session on my site from one class and then access the class to get the session values.
A typical property of the class would be something like this:
public int CustomerID
{
get
{
intCustomerID = Convert.ToInt32(HttpContex
return intCustomerID;
}
set
{
intCustomerID = value;
HttpContext.Current.Sessio
}
}
This was producing some errors in my logs "Parameter is not valid" because it seems that the class trying to get the Session["CustomerID"] was using it to insert into the database but i suspect that HttpContext.Current was null or is not available. In this case i would in addition get the error "Object reference not set to an instance of an object." because Session["CustomerID"] could not be accessed...
this could sometime be the case with these sessions and sometimes HttpContex may not be available for the class calling this property. So i modified the method like so:
public int CustomerID
{
get
{
if (!Int32.TryParse(HttpConte
{
intCustomerID = -1;
}
return intCustomerID;
}
set
{
intCustomerID = value;
HttpContext.Current.Sessio
}
}
Just to try and catch it it was available or not... As you can see this does not work and i get the same results...
So question is how do i check if HttpContext.Current is available or not and if not return a -1 value... I can't check if the session is null cause HttpContext.Current.Sessio
Hope that all made sense. Any ideas?
Nugs
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
There isn't any code I can think of. The only way Current or Session should be null is if you're calling the property too early in the page life cycle (perhaps the constructor, not sure) or if the property is part of a shared library that won't necessarily have a web user requesting access.
ASKER
So then something like this should work?
public int CustomerID
{
get
{
if (HttpContext.Current == null || HttpContext.Current.Session == null)
{
intCustomerID = -1;
}
else
{
if (!Int32.TryParse(HttpContext.Current.Session["CustomerID"].ToString(), out intCustomerID))
{
intCustomerID = -1;
}
}
return intCustomerID;
}
set
{
intCustomerID = value;
HttpContext.Current.Session["CustomerID"] = intCustomerID.ToString();
}
}
ASKER
Honestly i am not sure why it is not accessable... It could be that some classes calling this may not have web user requesting access. Either way i need to catch this at any point so that the inserts into the DB don't fail because of it...
Nugs
Nugs
ASKER
Think this will do it... Will have to check it in production though... Thanks!
ASKER
Nugs