I'm working on a IHttpModule that handles the PreSendRequestH eaders event
from the HttpApplication , if the event is raised after EndRequest then
HttpContext.Cur rent is null. If it is raised before EndRequest (by turning
response buffering off) then HttpContext.Cur rent is set.
To repo add the following to Global.asax
protected void Application_Pre SendRequestHead ers(object sender, EventArgs e)
{
Debug.WriteLine (MethodInfo.Get CurrentMethod() .Name + " -
HttpContext.Cur rent is null " + (HttpContext.Cu rrent == null));
Debug.WriteLine (MethodInfo.Get CurrentMethod() .Name + " -
HttpApplication .Context is null " + (((HttpApplicat ion)sender).Con text ==
null));
}
protected void Application_Pre SendRequestCont ent(object sender,
EventArgs e)
{
Debug.WriteLine (MethodInfo.Get CurrentMethod() .Name + " -
HttpContext.Cur rent is null " + (HttpContext.Cu rrent == null));
Debug.WriteLine (MethodInfo.Get CurrentMethod() .Name + " -
HttpApplication .Context is null " + (((HttpApplicat ion)sender).Con text ==
null));
}
protected void Application_Beg inRequest(objec t sender, EventArgs e)
{
Debug.WriteLine (MethodInfo.Get CurrentMethod() .Name + " -
HttpContext.Cur rent is null " + (HttpContext.Cu rrent == null));
Debug.WriteLine (MethodInfo.Get CurrentMethod() .Name + " -
HttpApplication .Context is null " + (((HttpApplicat ion)sender).Con text ==
null));
}
protected void Application_End Request(object sender, EventArgs e)
{
Debug.WriteLine (MethodInfo.Get CurrentMethod() .Name + " -
HttpContext.Cur rent is null " + (HttpContext.Cu rrent == null));
Debug.WriteLine (MethodInfo.Get CurrentMethod() .Name + " -
HttpApplication .Context is null " + (((HttpApplicat ion)sender).Con text ==
null));
}
It should write the following debug output...
Application_Beg inRequest - HttpContext.Cur rent is null False
Application_Beg inRequest - HttpApplication .Context is null False
Application_End Request - HttpContext.Cur rent is null False
Application_End Request - HttpApplication .Context is null False
Application_Pre SendRequestHead ers - HttpContext.Cur rent is null True
Application_Pre SendRequestHead ers - HttpApplication .Context is null False
Application_Pre SendRequestCont ent - HttpContext.Cur rent is null True
Application_Pre SendRequestCont ent - HttpApplication .Context is null False
Turn off Respose buffering by adding this line to the BegnRequest handler..
((HttpApplicati on)sender).Resp onse.Buffer = false;
Now the output is
Application_Beg inRequest - HttpContext.Cur rent is null False
Application_Beg inRequest - HttpApplication .Context is null False
Application_Pre SendRequestHead ers - HttpContext.Cur rent is null False
Application_Pre SendRequestHead ers - HttpApplication .Context is null False
Application_Pre SendRequestCont ent - HttpContext.Cur rent is null False
Application_Pre SendRequestCont ent - HttpApplication .Context is null False
Application_End Request - HttpContext.Cur rent is null False
Application_End Request - HttpApplication .Context is null False
(extra preSendRequestC ontent 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.Cur rent from the HttpApplication that raised the event as a
possible work around to what seems like a bug.
Anyone else experienced this ?