HttpContext.Current is Null in PreSendRequestHeaders

G

Guest

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 ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top