HttpContext.Current.Session is null

Discussion in 'ASP .Net' started by eric, Sep 20, 2007.

  1. eric

    eric Guest

    I have a 2.0 asp.net project. In a class contained within a seperate
    project, I am trying to reference HttpContext.Current.Session but Session is
    always null. I've tried implementing IRequiresSessionState but it does not
    seem to matter.
    For a test I created a small solution consisting of a web project and a
    seperate project to hold a test class. In the test class I reference
    HttpContext.Current.Session and it works fine with or without implementing
    IRequireSessionState. I'm unable to determine what I'm doing differently in
    my real project but cannot find it.

    Help would be appreciated.
    eric, Sep 20, 2007
    #1
    1. Advertising

  2. eric

    Teemu Keiski Guest

    Hi,

    is this referencing in class body or in some method/event handler? Since if
    it's at class body, especially in case of a Page, it is instantiated before
    Session is associated with the request


    --
    Teemu Keiski
    AspInsider, ASP.NET MVP
    http://blogs.aspadvice.com/joteke
    http://teemukeiski.net


    "eric" <7ak@!_df.com> wrote in message
    news:utoraz6%...
    >I have a 2.0 asp.net project. In a class contained within a seperate
    >project, I am trying to reference HttpContext.Current.Session but Session
    >is always null. I've tried implementing IRequiresSessionState but it does
    >not seem to matter.
    > For a test I created a small solution consisting of a web project and a
    > seperate project to hold a test class. In the test class I reference
    > HttpContext.Current.Session and it works fine with or without implementing
    > IRequireSessionState. I'm unable to determine what I'm doing differently
    > in my real project but cannot find it.
    >
    > Help would be appreciated.
    >
    >
    Teemu Keiski, Sep 20, 2007
    #2
    1. Advertising

  3. Hi there Eric,

    HttpContext ,as well as Session are created only for a HTTP request, meaning
    for any type of HTTP request served through a HTTP handler (i.e. a http
    handler that serves an aspx page, or a resource script etc.) In other words,
    it can only be accessed in web application when a request is being processed.
    I think you're trying to get session from a code that has nothing to do with
    web site.

    example:

    namespace MyLibrary
    {

    public static class MyClass
    {
    public static string GetSomeData()
    {
    return (string) HttpContext.Current.Session["SomeData"];
    }
    }

    }

    if i use it on the web page:

    protected void Page_Load(object sender, EventArgs e)
    {
    myLabel.Text = MyClass.GetSomeData();
    }

    it will work because any page is served through a HttpHandler that is
    responsible for creating the context and retreiving the session state if
    session state is switched on. If i used in windows application/windows
    service or any code which is not run in a HTTP context, it would get
    nullreferenceexception. There are few nice articels in the internet that will
    help you understand HttpContext and session basics, for instance:
    http://www.odetocode.com/Articles/112.aspx

    HTH
    --
    Milosz


    "eric" wrote:

    > I have a 2.0 asp.net project. In a class contained within a seperate
    > project, I am trying to reference HttpContext.Current.Session but Session is
    > always null. I've tried implementing IRequiresSessionState but it does not
    > seem to matter.
    > For a test I created a small solution consisting of a web project and a
    > seperate project to hold a test class. In the test class I reference
    > HttpContext.Current.Session and it works fine with or without implementing
    > IRequireSessionState. I'm unable to determine what I'm doing differently in
    > my real project but cannot find it.
    >
    > Help would be appreciated.
    >
    >
    >
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=, Sep 20, 2007
    #3
  4. What exactly is calling the separate project that is referencing
    Session? If the call is originating from a web request event then
    Session should be there. If the call is from another thread like a
    timer or threadpool or app created thread, then there will never be an
    active session.

    HTH,

    Sam

    ------------------------------------------------------------
    We're hiring! B-Line Medical is seeking .NET
    Developers for exciting positions in medical product
    development in MD/DC. Work with a variety of technologies
    in a relaxed team environment. See ads on Dice.com.



    On Thu, 20 Sep 2007 10:37:00 -0700, "eric" <7ak@!_df.com> wrote:

    >I have a 2.0 asp.net project. In a class contained within a seperate
    >project, I am trying to reference HttpContext.Current.Session but Session is
    >always null. I've tried implementing IRequiresSessionState but it does not
    >seem to matter.
    >For a test I created a small solution consisting of a web project and a
    >seperate project to hold a test class. In the test class I reference
    >HttpContext.Current.Session and it works fine with or without implementing
    >IRequireSessionState. I'm unable to determine what I'm doing differently in
    >my real project but cannot find it.
    >
    >Help would be appreciated.
    >
    Samuel R. Neff, Sep 20, 2007
    #4
  5. eric

    eric Guest

    It's within a method of a singleton class.

    "Teemu Keiski" <> wrote in message
    news:edWx$y7%...
    > Hi,
    >
    > is this referencing in class body or in some method/event handler? Since
    > if it's at class body, especially in case of a Page, it is instantiated
    > before Session is associated with the request
    >
    >
    > --
    > Teemu Keiski
    > AspInsider, ASP.NET MVP
    > http://blogs.aspadvice.com/joteke
    > http://teemukeiski.net
    >
    >
    > "eric" <7ak@!_df.com> wrote in message
    > news:utoraz6%...
    >>I have a 2.0 asp.net project. In a class contained within a seperate
    >>project, I am trying to reference HttpContext.Current.Session but Session
    >>is always null. I've tried implementing IRequiresSessionState but it does
    >>not seem to matter.
    >> For a test I created a small solution consisting of a web project and a
    >> seperate project to hold a test class. In the test class I reference
    >> HttpContext.Current.Session and it works fine with or without
    >> implementing IRequireSessionState. I'm unable to determine what I'm
    >> doing differently in my real project but cannot find it.
    >>
    >> Help would be appreciated.
    >>
    >>

    >
    >
    eric, Sep 20, 2007
    #5
  6. eric

    eric Guest

    I understand what your saying, however it appears to work exactly as
    expected within my test below:.

    This is the test class I'm using. This class is in a seperate project
    created as a class library

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Web;
    using System.Web.SessionState;

    public class CProjTest
    {
    private static readonly CProjTest Instance = new CProjTest();

    private CProjTest()
    {
    }
    static CProjTest()
    {
    }
    public static CProjTest GetInstance()
    {
    return Instance;
    }
    public void Test()
    {
    HttpSessionState sess = HttpContext.Current.Session;
    string s1 = (string)sess["somevar"];
    }
    }

    This is the call from my test asp.net project's default.aspx page

    public partial class _Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    HttpSessionState sess = HttpContext.Current.Session;
    sess["somevar"] = "Test";
    CProjTest cpt = CProjTest.GetInstance();
    cpt.Test();
    }
    }


    Effectively, there is little difference in the above test solution and my
    real project and the above works without a problem.


    "Milosz Skalecki [MCAD]" <> wrote in message
    news:...
    > Hi there Eric,
    >
    > HttpContext ,as well as Session are created only for a HTTP request,
    > meaning
    > for any type of HTTP request served through a HTTP handler (i.e. a http
    > handler that serves an aspx page, or a resource script etc.) In other
    > words,
    > it can only be accessed in web application when a request is being
    > processed.
    > I think you're trying to get session from a code that has nothing to do
    > with
    > web site.
    >
    > example:
    >
    > namespace MyLibrary
    > {
    >
    > public static class MyClass
    > {
    > public static string GetSomeData()
    > {
    > return (string) HttpContext.Current.Session["SomeData"];
    > }
    > }
    >
    > }
    >
    > if i use it on the web page:
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > myLabel.Text = MyClass.GetSomeData();
    > }
    >
    > it will work because any page is served through a HttpHandler that is
    > responsible for creating the context and retreiving the session state if
    > session state is switched on. If i used in windows application/windows
    > service or any code which is not run in a HTTP context, it would get
    > nullreferenceexception. There are few nice articels in the internet that
    > will
    > help you understand HttpContext and session basics, for instance:
    > http://www.odetocode.com/Articles/112.aspx
    >
    > HTH
    > --
    > Milosz
    >
    >
    > "eric" wrote:
    >
    >> I have a 2.0 asp.net project. In a class contained within a seperate
    >> project, I am trying to reference HttpContext.Current.Session but Session
    >> is
    >> always null. I've tried implementing IRequiresSessionState but it does
    >> not
    >> seem to matter.
    >> For a test I created a small solution consisting of a web project and a
    >> seperate project to hold a test class. In the test class I reference
    >> HttpContext.Current.Session and it works fine with or without
    >> implementing
    >> IRequireSessionState. I'm unable to determine what I'm doing differently
    >> in
    >> my real project but cannot find it.
    >>
    >> Help would be appreciated.
    >>
    >>
    >>
    eric, Sep 20, 2007
    #6
  7. eric

    eric Guest

    This is the test class I'm using. This class is in a seperate project
    created as a class library

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Web;
    using System.Web.SessionState;

    public class CProjTest
    {
    private static readonly CProjTest Instance = new CProjTest();

    private CProjTest()
    {
    }
    static CProjTest()
    {
    }
    public static CProjTest GetInstance()
    {
    return Instance;
    }
    public void Test()
    {
    HttpSessionState sess = HttpContext.Current.Session;
    string s1 = (string)sess["somevar"];
    }
    }

    This is the call from my test asp.net project's default.aspx page

    public partial class _Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    HttpSessionState sess = HttpContext.Current.Session;
    sess["somevar"] = "Test";
    CProjTest cpt = CProjTest.GetInstance();
    cpt.Test();
    }
    }


    Effectively, there is little difference in the above test solution and my
    real project and the above works without a problem.


    "Samuel R. Neff" <> wrote in message
    news:...
    >
    > What exactly is calling the separate project that is referencing
    > Session? If the call is originating from a web request event then
    > Session should be there. If the call is from another thread like a
    > timer or threadpool or app created thread, then there will never be an
    > active session.
    >
    > HTH,
    >
    > Sam
    >
    > ------------------------------------------------------------
    > We're hiring! B-Line Medical is seeking .NET
    > Developers for exciting positions in medical product
    > development in MD/DC. Work with a variety of technologies
    > in a relaxed team environment. See ads on Dice.com.
    >
    >
    >
    > On Thu, 20 Sep 2007 10:37:00 -0700, "eric" <7ak@!_df.com> wrote:
    >
    >>I have a 2.0 asp.net project. In a class contained within a seperate
    >>project, I am trying to reference HttpContext.Current.Session but Session
    >>is
    >>always null. I've tried implementing IRequiresSessionState but it does
    >>not
    >>seem to matter.
    >>For a test I created a small solution consisting of a web project and a
    >>seperate project to hold a test class. In the test class I reference
    >>HttpContext.Current.Session and it works fine with or without implementing
    >>IRequireSessionState. I'm unable to determine what I'm doing differently
    >>in
    >>my real project but cannot find it.
    >>
    >>Help would be appreciated.
    >>

    >
    eric, Sep 20, 2007
    #7
  8. If the test works and the real one doesn't then there must be some
    difference. Please post the smallest portion of your real code that
    demonstrates the problem?

    Sam

    ------------------------------------------------------------
    We're hiring! B-Line Medical is seeking .NET
    Developers for exciting positions in medical product
    development in MD/DC. Work with a variety of technologies
    in a relaxed team environment. See ads on Dice.com.


    On Thu, 20 Sep 2007 14:24:47 -0700, "eric" <7ak@!_df.com> wrote:

    >This is the test class I'm using. This class is in a seperate project
    >created as a class library
    >
    > ...
    >
    >Effectively, there is little difference in the above test solution and my
    >real project and the above works without a problem.
    >
    Samuel R. Neff, Sep 21, 2007
    #8
  9. Were you able to figure out the problem with Session being Null

    Thanks
    Kishore

    *** Sent via Developersdex http://www.developersdex.com ***
    kishore vanapalli, Oct 2, 2007
    #9
    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. Replies:
    2
    Views:
    712
    William F. Robertson, Jr.
    May 16, 2005
  2. =?Utf-8?B?RGF2ZQ==?=

    HttpContext.Current is null in asynch method?

    =?Utf-8?B?RGF2ZQ==?=, Oct 31, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    4,689
    Joerg Jooss
    Nov 2, 2005
  3. Dave
    Replies:
    2
    Views:
    12,836
    Alexey Smirnov
    May 16, 2007
  4. R.A.M.
    Replies:
    14
    Views:
    1,925
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    Oct 17, 2007
  5. Tony

    Dim context As HttpContext = HttpContext.Current

    Tony, Mar 2, 2004, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    285
    Teemu Keiski
    Mar 3, 2004
Loading...

Share This Page