Oddur Magnusson

Object reference not set to an instance of a human being

HttpContext.Current

System.Web.HttpContext Context = System.Web.HttpContext.Current; !

For a while every time I created a function that looked something up in cache or Context.Items, I used to pass the Context object as a parameter to do the lookup on.  Then I hadn't discovered the System.Web.HttpContext.Current object, it returns a reference to the currently executing HttpContext. Why hadn't someone told me about this one ! ;)

Oh, the joy of refactoring !

Posted: Feb 11 2004, 12:48 PM by oddurmag | with 6 comment(s)
Filed under:

Comments

Derick Bailey said:

I think it's faster to passt he context object, rather than looking it up by this method. When you pass the reference, all you are doing is sending a pointer down a chain of variables. But when you look up the object using .Current, the runtime has to run a bunch of code in order to find out what the current context is, and then return that reference to you. If you make multiple calls to .Current, then you are really wasting processing time, because it has to run the same code to find the current context each time you call .Current.
# February 11, 2004 9:17 AM

Scott Watermasysk said:

I agree with Derick. I think a better approach is a simple overload. See http://scottwater.com/blog/archive/2004/02/11/HttpContextOverload.aspx

-Scott
# February 11, 2004 9:49 AM

Stefán Jökull said:

Well.. after a little looksy with Lutz Roeder's Reflector i saw that HttpContext.Current value is in the end being fetched from a Hashtable. The code chain is relatively small so i'd think this would be quite fast enough. If you're in doubt just try timing a couple of hundred accesses to the variable.

P.S.
I did a little test and called HttpContext.Current and assigned it to a variable a 100.000 times inside a simple FOR loop.

The results:
It took a "whopping" 30 - 40 milliseconds to call it a 100.000 times (running Windows XP, local IIS, 1GB RAM, P4 1.7Ghz). So i guess it's quite fast enough :)
# February 11, 2004 10:20 AM

AndrewSeven said:

If I am going to use it a bunch of times in the same function then I create the local variable context.

System.Web.HttpContext context = System.Web.HttpContext.Current;

I think/hope this eliminates the hastable lookup
# February 11, 2004 10:49 AM

Stefán Jökull said:

AndrewSeven:
If you're going to use it many times then yes of course you should create a variable for it (that's what variables are for :). But my main point was that the cost of calling HttpContext.Current once per function (like Derick and Scott seemed to be worried about) is very very tiny. So small that you should by no means worry about it (we're talking about a few microseconds per call).
# February 11, 2004 11:20 AM

Rick Strahl said:

Ha ha... some people worry waaaaaaaaaaaaaaaaaaay too much about perf in the wrong places. Oh, no a onetime hashtable lookup. If anybody's worried about that start using Response.Write() for your output.

HttpContext.Current is too useful in keeping code self contained and in reducing verbosity. I too found out about this long after I should have known and ended up refactoring tons of code.
# February 12, 2004 4:34 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)