468,114 Members | 2,029 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,114 developers. It's quick & easy.

HttpContext.Current is Null in PreSendRequestHeaders

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
0 4608

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Mat | last post: by
2 posts views Thread by Luis Esteban Valencia Muñoz | last post: by
4 posts views Thread by Sami Rehman | last post: by
2 posts views Thread by Dave | last post: by
3 posts views Thread by Madhur | last post: by
3 posts views Thread by didacticone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.