473,797 Members | 3,183 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can HttpContext.Cur rent be null

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.Cur rent to be null
in my business logic. I have extensively used Cache and Session objects, and
currently I assume HttpContext.Cur rent object to be existent.

Also, since I do not have this check, some of my unit test cases fail
because there is no HttpContext for them.

How do you handle this scenario. ?

Should I include check for existence of HttpContext.Cur rent and may be even
more for Session and Cache objects ?

--
Madhur
Jul 8 '08 #1
3 8912
HttpContext is defined by the asp.net request processor. it will be null
if your classes are not referenced from a web request. it will be null
in a unit test, for a control loaded in the designer, or if your code
starts a background thread, it will be null in the new thread.

-- bruce (sqlwork.com)

Madhur wrote:
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.Cur rent to be null
in my business logic. I have extensively used Cache and Session objects, and
currently I assume HttpContext.Cur rent object to be existent.

Also, since I do not have this check, some of my unit test cases fail
because there is no HttpContext for them.

How do you handle this scenario. ?

Should I include check for existence of HttpContext.Cur rent and may be even
more for Session and Cache objects ?

--
Madhur

Jul 8 '08 #2
Hi Bruce

Thanks for the info.

Do you have any recommendations on how to go about the unit testing of the
procedures which make use of
HttpContext, session or Cache objects.

Thanks in Advance.

Madhur

"bruce barker" <no****@nospam. comwrote in message
news:#P******** ******@TK2MSFTN GP06.phx.gbl...
HttpContext is defined by the asp.net request processor. it will be null
if your classes are not referenced from a web request. it will be null in
a unit test, for a control loaded in the designer, or if your code starts
a background thread, it will be null in the new thread.

-- bruce (sqlwork.com)

Madhur wrote:
>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.Cur rent to be
null
in my business logic. I have extensively used Cache and Session objects,
and currently I assume HttpContext.Cur rent object to be existent.

Also, since I do not have this check, some of my unit test cases fail
because there is no HttpContext for them.

How do you handle this scenario. ?

Should I include check for existence of HttpContext.Cur rent and may be
even more for Session and Cache objects ?

--
Madhur
Jul 8 '08 #3
Like Bruce said earlier, HttpContext.Cur rent can be null - however accessing
the contextual information from an ASP.NET page or user control will most
likely ensure that it never will be null. HttpContext.Cur rent really depends
when it's being accessed to determine if it contains any data.

As for your followup question, just structure your code so it won't rely on
having contextual information available. As long as the context information
is only used from your website you shouldn't have any problems writing unit
tests for your business logic. Also, not requiring the context information
would allow you to reuse the assemblies (assuming you have your business
logic separated from your presentation layer) in other applications by
simply referencing the assembly.

void DoSomething() {
int id = (int)HttpContex t.Current.Sessi on["id"];

// Use the id.
}

void DoSomething(int id) {
// Use the id.
}

protected void Page_Load(objec t sender, EventArgs e) {
this.DoSomethin g((int)this.Ses sion["id"]);
}

You can see both methods do the exact same thing, one of them just doesn't
rely on context information while the other does.

"Madhur" <sd*@df.comwrot e in message
news:DE******** *************** ***********@mic rosoft.com...
Hi Bruce

Thanks for the info.

Do you have any recommendations on how to go about the unit testing of the
procedures which make use of
HttpContext, session or Cache objects.

Thanks in Advance.

Madhur

"bruce barker" <no****@nospam. comwrote in message
news:#P******** ******@TK2MSFTN GP06.phx.gbl...
>HttpContext is defined by the asp.net request processor. it will be null
if your classes are not referenced from a web request. it will be null in
a unit test, for a control loaded in the designer, or if your code starts
a background thread, it will be null in the new thread.

-- bruce (sqlwork.com)

Madhur wrote:
>>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.Cur rent to be
null
in my business logic. I have extensively used Cache and Session objects,
and currently I assume HttpContext.Cur rent object to be existent.

Also, since I do not have this check, some of my unit test cases fail
because there is no HttpContext for them.

How do you handle this scenario. ?

Should I include check for existence of HttpContext.Cur rent and may be
even more for Session and Cache objects ?

--
Madhur
Jul 10 '08 #4

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

Similar topics

2
3346
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 HttpContext.Current.Session("CustDataSet") = GetCustDataSet()
4
5370
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
4314
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
2154
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.
0
5409
by: Aaron Morton | last post by:
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) {...
4
28034
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
12020
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 interfering with my ability to access the session.
14
29484
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 Admin {
0
2283
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 encountered. the line of error is HTTPcontext context=HTTPcontext.Current; it goes in the else part of the...
0
10469
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10246
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10209
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 most users, this new feature is actually very convenient. If you want to control the update process,...
1
7560
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6803
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5459
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4135
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3750
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2934
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.