Solved

Make sure HttpContext.Current is valid and available

Posted on 2008-06-17
6
1,157 Views
Last Modified: 2010-04-21
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(HttpContext.Current.Session["CustomerID"].ToString());
                return intCustomerID;
            }
            set
            {
                intCustomerID = value;
                HttpContext.Current.Session["CustomerID"] = 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(HttpContext.Current.Session["CustomerID"].ToString(), out intCustomerID))
                {
                    intCustomerID = -1;
                }

                return intCustomerID;
            }
            set
            {
                intCustomerID = value;
                HttpContext.Current.Session["CustomerID"] = 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.Session is not available...

Hope that all made sense. Any ideas?

Nugs
0
Comment
Question by:Nugs
  • 4
  • 2
6 Comments
 
LVL 15

Accepted Solution

by:
crisco96 earned 500 total points
ID: 21806333
Just see if Current and Session are null
if(HttpContext.Current == null || HttpContext.Current.Session == null)

{

    return -1;

}

Open in new window

0
 
LVL 2

Author Comment

by:Nugs
ID: 21806372
Next question... is there a way (some code) that i can use in testing that will force HttpContext.Current to become null... I can't seem to reproduce these errors locally and only see them in my logs... Well actually the lack of logs as this is what seems to be failing....

Nugs
0
 
LVL 15

Expert Comment

by:crisco96
ID: 21806550
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.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 2

Author Comment

by:Nugs
ID: 21806562
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();

            }

        }

Open in new window

0
 
LVL 2

Author Comment

by:Nugs
ID: 21806580
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
0
 
LVL 2

Author Closing Comment

by:Nugs
ID: 31468101
Think this will do it... Will have to check it in production though... Thanks!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Aspnet xml 2 36
Unit testing, moq, best practices 7 29
c# code 19 61
Why is "packages element is not declared"? 2 42
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now