472,783 Members | 1,104 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,783 software developers and data experts.

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 5146

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: moondaddy | last post by:
I need to set a variable to a session variable (if that's what you call it) like this: dim ds as dataset = HttpContext.Current.Session("CustDataSet") But I get an exception if this variable...
4
by: Mat | last post by:
Hi, I've stumbled onto a problem when using the caching object in ASP.Net. I'm placing a static dataset to the cache as the data only changes once a day. Whilst writing to the cache I'm using...
2
by: Luis Esteban Valencia Muñoz | last post by:
I have a 2 base classes that do error handling -- one for pages (System.Web.UI.Page) and one for applications (System.Web.HttpApplication, Global.asax uses it). Are there any situations in...
2
by: | last post by:
Today I learned that creating cookies inside of a custom class in ASP.NET 2.0 requires that you prefix it with HttpContext.Current..., e.g. : ...
4
by: Sami Rehman | last post by:
hi i have a vs solution in which there are 2 projects class library representing Security services and another one is web application project. i need to access http context in a static method in...
2
by: Dave | last post by:
After some digging, I discovered HttpContext.Current.Session is null when trying to access a session variable, username, in my upload.cs code which is in the App_Code folder. I just determined...
14
by: R.A.M. | last post by:
Hello, I have created ASP.NET project in which I have a file Admin.cs. It contains static class Admin with some methods and properties. The problem is that in property get a reference...
0
by: alister7 | last post by:
hi every1 im trying to download a music file from the database SQLserver.which i upload in the database.. The Code below works fine in wen i create a new project of an ASP.Net web application..m...
3
by: Madhur | last post by:
Hello I am delivering an asp.net 2.0 application to my customer. I need to know, If I need to check for the condition of HttpContext.Current to be null in my business logic. I have extensively...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.