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)
?
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
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
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.
精彩评论