473,583 Members | 3,010 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

HttpContext.Cur rent is Null in PreSendRequestH eaders

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 ?
Nov 9 '06 #1
0 5361

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

Similar topics

2
3323
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 in the current session hasn't been set yet so I need to test it. I tried: If HttpContext.Current.Session("CustDataSet") = nothing then...
4
5352
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 a lock using code like below (just typed this in); Cache thisCache = HttpContext.Current.Cache lock(thisCache)
2
4294
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 either of these error handlers where HttpContext.Current would be null? Page Error Handler public class PageBase : System.Web.UI.Page {
2
2132
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. : HttpContext.Current.Response.Cookies.Add("myNewCookie"); I am wondering if there are any landmines that I should know about, or if this will work pretty much as I am expecting a cookie should.
4
27985
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 a class of Security Services project. how can this be done. thanks -sami
2
11959
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 that I can't because HttpContext.Current.Session is null. (HttpContext.Current is fine though) I think there may be another server side method...
14
29438
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 HttpContext.Current.Session is null, although for instance HttpContext.Current.Application or HttpContext.Current.Request is not null: public static class...
0
2262
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 using VS 2008 but it gives me an error wen i run the same code in WPF broser APplication(xbap).. it throws an error saying Null reference...
3
8886
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 used Cache and Session objects, and currently I assume HttpContext.Current object to be existent. Also, since I do not have this check, some of my...
0
7895
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7826
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8182
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8327
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7935
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5701
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5374
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3818
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1433
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.