By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,671 Members | 1,633 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,671 IT Pros & Developers. It's quick & easy.

HttpContext.Current is Null in PreSendRequestHeaders

P: n/a
I'm working on a IHttpModule that handles the PreSendRequestHeaders event
from the HttpApplication, if the event is raised after EndRequest then
HttpContext.Current is null. If it is raised before EndRequest (by turning
response buffering off) then HttpContext.Current is set.

To repo add the following to Global.asax
protected void Application_PreSendRequestHeaders(object sender, EventArgs e)
{
Debug.WriteLine(MethodInfo.GetCurrentMethod().Name + " -
HttpContext.Current is null " + (HttpContext.Current == null));
Debug.WriteLine(MethodInfo.GetCurrentMethod().Name + " -
HttpApplication.Context is null " + (((HttpApplication)sender).Context ==
null));
}

protected void Application_PreSendRequestContent(object sender,
EventArgs e)
{
Debug.WriteLine(MethodInfo.GetCurrentMethod().Name + " -
HttpContext.Current is null " + (HttpContext.Current == null));
Debug.WriteLine(MethodInfo.GetCurrentMethod().Name + " -
HttpApplication.Context is null " + (((HttpApplication)sender).Context ==
null));
}

protected void Application_BeginRequest(object sender, EventArgs e)
{
Debug.WriteLine(MethodInfo.GetCurrentMethod().Name + " -
HttpContext.Current is null " + (HttpContext.Current == null));
Debug.WriteLine(MethodInfo.GetCurrentMethod().Name + " -
HttpApplication.Context is null " + (((HttpApplication)sender).Context ==
null));

}

protected void Application_EndRequest(object sender, EventArgs e)
{
Debug.WriteLine(MethodInfo.GetCurrentMethod().Name + " -
HttpContext.Current is null " + (HttpContext.Current == null));
Debug.WriteLine(MethodInfo.GetCurrentMethod().Name + " -
HttpApplication.Context is null " + (((HttpApplication)sender).Context ==
null));

}

It should write the following debug output...
Application_BeginRequest - HttpContext.Current is null False
Application_BeginRequest - HttpApplication.Context is null False
Application_EndRequest - HttpContext.Current is null False
Application_EndRequest - HttpApplication.Context is null False
Application_PreSendRequestHeaders - HttpContext.Current is null True
Application_PreSendRequestHeaders - HttpApplication.Context is null False
Application_PreSendRequestContent - HttpContext.Current is null True
Application_PreSendRequestContent - HttpApplication.Context is null False

Turn off Respose buffering by adding this line to the BegnRequest handler..
((HttpApplication)sender).Response.Buffer = false;

Now the output is
Application_BeginRequest - HttpContext.Current is null False
Application_BeginRequest - HttpApplication.Context is null False
Application_PreSendRequestHeaders - HttpContext.Current is null False
Application_PreSendRequestHeaders - HttpApplication.Context is null False
Application_PreSendRequestContent - HttpContext.Current is null False
Application_PreSendRequestContent - HttpApplication.Context is null False
Application_EndRequest - HttpContext.Current is null False
Application_EndRequest - HttpApplication.Context is null False

(extra preSendRequestContent event removed)

This is inconsistent and i'm wonder what other thread local settings may not
be set when the event is raised after EndRequest. I can set the
HttpContext.Current from the HttpApplication that raised the event as a
possible work around to what seems like a bug.

Anyone else experienced this ?
Nov 9 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.