473,320 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 5290

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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.