Access SessionState in Business Tier

G

Glenn

Hi,

I know that I can access session state on an asp.net page
using either HttpContext or Page objects, but how do I
access session data from middle tiers?

Should the technique be to extricate the necessary session
data with a web page and pass this to the business tier?

If I need to alter session state in a middle tier object,
should I use a reference parameter and update the session
state when the method call is complete?

Thanks,

Glenn.
 
S

Steve C. Orr [MVP, MCSD]

You could just use the HttpContext object from your business tier.
Or if you want your business objects to be more generic so they could be
used from other (non-web) front ends in the future then you may just want to
only pass value types as parameters to your business objects. In this case
your page objects would extract the necessary information from session state
so the business objects don't need to deal with any web stuff.
 
G

Glenn

My initial thoughts were that the HTTPContext object would
not be in scope in a middle tier object method, hence
passing individual session values as method parameters. Is
this correct?

Here is a subset of the sample I knocked up to test this
scenario:

public string GetTypes()
{
using System.Web;
StringBuilder sb = new StringBuilder();
sb.Append(HttpContext.Session["UserID"].ToString());
return(sb.ToString());
}

The following message is generated:

An object reference is required for the nonstatic field,
method, or property 'System.Web.HttpContext.Session'.

Do I need to pass the HTTPContext object to the middle
tier object through a method parameter?
 
H

Hermit Dave

yes it would not be in the scope of middle tier... but you can always have a
member function of middle tier taking in Context object as a ref param and
same applies with Session.
if you dont want to make any modifications... you could try passing it a
clone...

--
Regards,

HD

Glenn said:
My initial thoughts were that the HTTPContext object would
not be in scope in a middle tier object method, hence
passing individual session values as method parameters. Is
this correct?

Here is a subset of the sample I knocked up to test this
scenario:

public string GetTypes()
{
using System.Web;
StringBuilder sb = new StringBuilder();
sb.Append(HttpContext.Session["UserID"].ToString());
return(sb.ToString());
}

The following message is generated:

An object reference is required for the nonstatic field,
method, or property 'System.Web.HttpContext.Session'.

Do I need to pass the HTTPContext object to the middle
tier object through a method parameter?
-----Original Message-----
You could just use the HttpContext object from your business tier.
Or if you want your business objects to be more generic so they could be
used from other (non-web) front ends in the future then you may just want to
only pass value types as parameters to your business objects. In this case
your page objects would extract the necessary information from session state
so the business objects don't need to deal with any web stuff.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
Hire top-notch developers at http://www.able- consulting.com






.
 
S

Steve C. Orr [MVP, MCSD]

You have the wrong syntax.
Instead, use System.Web.HttpContext.Current.Session

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
Hire top-notch developers at http://www.able-consulting.com




Glenn said:
My initial thoughts were that the HTTPContext object would
not be in scope in a middle tier object method, hence
passing individual session values as method parameters. Is
this correct?

Here is a subset of the sample I knocked up to test this
scenario:

public string GetTypes()
{
using System.Web;
StringBuilder sb = new StringBuilder();
sb.Append(HttpContext.Session["UserID"].ToString());
return(sb.ToString());
}

The following message is generated:

An object reference is required for the nonstatic field,
method, or property 'System.Web.HttpContext.Session'.

Do I need to pass the HTTPContext object to the middle
tier object through a method parameter?
-----Original Message-----
You could just use the HttpContext object from your business tier.
Or if you want your business objects to be more generic so they could be
used from other (non-web) front ends in the future then you may just want to
only pass value types as parameters to your business objects. In this case
your page objects would extract the necessary information from session state
so the business objects don't need to deal with any web stuff.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
Hire top-notch developers at http://www.able- consulting.com






.
 
G

Glenn

Is this technique commonly used? To the inexperienced eye
it seems fairly expensive...
-----Original Message-----
yes it would not be in the scope of middle tier... but you can always have a
member function of middle tier taking in Context object as a ref param and
same applies with Session.
if you dont want to make any modifications... you could try passing it a
clone...

--
Regards,

HD

My initial thoughts were that the HTTPContext object would
not be in scope in a middle tier object method, hence
passing individual session values as method parameters. Is
this correct?

Here is a subset of the sample I knocked up to test this
scenario:

public string GetTypes()
{
using System.Web;
StringBuilder sb = new StringBuilder();
sb.Append(HttpContext.Session["UserID"].ToString());
return(sb.ToString());
}

The following message is generated:

An object reference is required for the nonstatic field,
method, or property 'System.Web.HttpContext.Session'.

Do I need to pass the HTTPContext object to the middle
tier object through a method parameter?
-----Original Message-----
You could just use the HttpContext object from your business tier.
Or if you want your business objects to be more generic so they could be
used from other (non-web) front ends in the future then you may just want to
only pass value types as parameters to your business objects. In this case
your page objects would extract the necessary
information
from session state
so the business objects don't need to deal with any web stuff.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
Hire top-notch developers at http://www.able- consulting.com



Hi,

I know that I can access session state on an asp.net page
using either HttpContext or Page objects, but how do I
access session data from middle tiers?

Should the technique be to extricate the necessary session
data with a web page and pass this to the business tier?

If I need to alter session state in a middle tier object,
should I use a reference parameter and update the session
state when the method call is complete?

Thanks,

Glenn.


.


.
 
G

Glenn

Thanks for your help, works well now without having to
pass any parameters around... much appreciated.
-----Original Message-----
You have the wrong syntax.
Instead, use System.Web.HttpContext.Current.Session

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
Hire top-notch developers at http://www.able- consulting.com




My initial thoughts were that the HTTPContext object would
not be in scope in a middle tier object method, hence
passing individual session values as method parameters. Is
this correct?

Here is a subset of the sample I knocked up to test this
scenario:

public string GetTypes()
{
using System.Web;
StringBuilder sb = new StringBuilder();
sb.Append(HttpContext.Session["UserID"].ToString());
return(sb.ToString());
}

The following message is generated:

An object reference is required for the nonstatic field,
method, or property 'System.Web.HttpContext.Session'.

Do I need to pass the HTTPContext object to the middle
tier object through a method parameter?
-----Original Message-----
You could just use the HttpContext object from your business tier.
Or if you want your business objects to be more generic so they could be
used from other (non-web) front ends in the future then you may just want to
only pass value types as parameters to your business objects. In this case
your page objects would extract the necessary
information
from session state
so the business objects don't need to deal with any web stuff.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
Hire top-notch developers at http://www.able- consulting.com



Hi,

I know that I can access session state on an asp.net page
using either HttpContext or Page objects, but how do I
access session data from middle tiers?

Should the technique be to extricate the necessary session
data with a web page and pass this to the business tier?

If I need to alter session state in a middle tier object,
should I use a reference parameter and update the session
state when the method call is complete?

Thanks,

Glenn.


.


.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top