HttpContext.Current.Session is null

E

eric

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

Teemu Keiski

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
 
G

Guest

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
 
S

Samuel R. Neff

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
 
E

eric

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 said:
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 said:
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.
 
E

eric

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

Samuel R. Neff

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
 
K

kishore vanapalli

Were you able to figure out the problem with Session being Null

Thanks
Kishore
 

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

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top