473,405 Members | 2,154 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,405 software developers and data experts.

Can HttpContext.Current 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.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 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.Current and may be even
more for Session and Cache objects ?

--
Madhur
Jul 8 '08 #1
3 8858
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.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 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.Current 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**************@TK2MSFTNGP06.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.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 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.Current and may be
even more for Session and Cache objects ?

--
Madhur
Jul 8 '08 #3
Like Bruce said earlier, HttpContext.Current 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.Current 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)HttpContext.Current.Session["id"];

// Use the id.
}

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

protected void Page_Load(object sender, EventArgs e) {
this.DoSomething((int)this.Session["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.comwrote in message
news:DE**********************************@microsof t.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**************@TK2MSFTNGP06.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.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 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.Current 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
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. : ...
0
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...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.