Re: HttpContext.Current.Session is null

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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]" <mily242@xxxxxxxxxxxxxxxxx> wrote in message
news:C331094F-3FAD-4DB9-AD2A-9BD3BD4C185C@xxxxxxxxxxxxxxxx
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.





.



Relevant Pages