nickytonline
nickytonline

Reputation: 6981

Is HttpContext.Current Ever Null in a Web Application?

Is HttpContext.Current ever null in a web Application (assuming threads are not being used)? More specifically, would it ever be null in void Application_OnError(object sender, EventArgs e)?

Upvotes: 2

Views: 2364

Answers (3)

x0n
x0n

Reputation: 52480

There were some interesting changes in IIS 7 with respect to this. In IIS6, you had a HttpContext in Application_Start. From IIS7 onwards this is no longer the case.

More Information:

http://blogs.msdn.com/webtopics/archive/2009/02/12/webbaseevent-raise-method-fails-in-application-start-event-with-a-nullreferenceexception-on-iis-7-0.aspx

-Oisin

Upvotes: 2

slolife
slolife

Reputation: 19870

I believe it can happen if, for example, you spin off a worker thread, the response ends, and the worker thread throws an exception. Your Application_OnError will catch the exception, but the HttpContext.Current will be null.

Upvotes: 1

codeulike
codeulike

Reputation: 23064

see this other question

Session is not initialized until the AcquireRequestState event, so any error occurring before this point will not have session variables available.

I think the same applies to HttpContext.Current as HttpContext.Current.Session

So yes, I think HttpContext.Current can sometimes be null in Application_OnError

Upvotes: 3

Related Questions