Access SessionState in Business Tier

Discussion in 'ASP .Net' started by Glenn, Jan 6, 2004.

  1. Glenn

    Glenn Guest

    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.
     
    Glenn, Jan 6, 2004
    #1
    1. Advertising

  2. 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



    "Glenn" <> wrote in message
    news:036c01c3d4aa$6f2db0a0$...
    > 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.
     
    Steve C. Orr [MVP, MCSD], Jan 6, 2004
    #2
    1. Advertising

  3. Glenn

    Glenn Guest

    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
    >
    >
    >
    >"Glenn" <> wrote in

    message
    >news:036c01c3d4aa$6f2db0a0$...
    >> 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.

    >
    >
    >.
    >
     
    Glenn, Jan 6, 2004
    #3
  4. Glenn

    Hermit Dave Guest

    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" <> wrote in message
    news:03f701c3d4b0$17b24ec0$...
    > 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
    > >
    > >
    > >
    > >"Glenn" <> wrote in

    > message
    > >news:036c01c3d4aa$6f2db0a0$...
    > >> 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.

    > >
    > >
    > >.
    > >
     
    Hermit Dave, Jan 6, 2004
    #4
  5. 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" <> wrote in message
    news:03f701c3d4b0$17b24ec0$...
    > 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
    > >
    > >
    > >
    > >"Glenn" <> wrote in

    > message
    > >news:036c01c3d4aa$6f2db0a0$...
    > >> 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.

    > >
    > >
    > >.
    > >
     
    Steve C. Orr [MVP, MCSD], Jan 7, 2004
    #5
  6. Glenn

    Glenn Guest

    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
    >
    >"Glenn" <> wrote in

    message
    >news:03f701c3d4b0$17b24ec0$...
    >> 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
    >> >
    >> >
    >> >
    >> >"Glenn" <> wrote in

    >> message
    >> >news:036c01c3d4aa$6f2db0a0$...
    >> >> 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.
    >> >
    >> >
    >> >.
    >> >

    >
    >
    >.
    >
     
    Glenn, Jan 7, 2004
    #6
  7. Glenn

    Glenn Guest

    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
    >
    >
    >
    >
    >"Glenn" <> wrote in

    message
    >news:03f701c3d4b0$17b24ec0$...
    >> 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
    >> >
    >> >
    >> >
    >> >"Glenn" <> wrote in

    >> message
    >> >news:036c01c3d4aa$6f2db0a0$...
    >> >> 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.
    >> >
    >> >
    >> >.
    >> >

    >
    >
    >.
    >
     
    Glenn, Jan 7, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rob

    ASP v2 & 3-tier or 2-tier

    rob, Aug 13, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    429
    Steve C. Orr [MVP, MCSD]
    Aug 13, 2004
  2. NOSPAM

    2 tier to 3 tier?

    NOSPAM, Oct 14, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    461
    =?Utf-8?B?Q293Ym95IChHcmVnb3J5IEEuIEJlYW1lcikgLSBN
    Oct 14, 2004
  3. Steve Kershaw

    Setting up a new tier in a 3-tier system?

    Steve Kershaw, Mar 28, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    470
    Steve Kershaw
    Mar 29, 2006
  4. Shantanu Bhattacharya
    Replies:
    2
    Views:
    586
    Christopher Benson-Manica
    Dec 26, 2003
  5. Nemisis
    Replies:
    1
    Views:
    515
    sloan
    Aug 22, 2006
Loading...

Share This Page