Weird... Session[] not working from static classes (IIS only problem)

B

Benton

Hi there,

I'm having a problem with a method which resides on a static class I have on
my ASP.NET application.

public static string FilterCenter(string fieldName)
{

string centers =
((User)System.Web.HttpContext.Current.Session["userdata"]).Centers;

...some more code...
}

The point here is how I cast Session["userdata"] to the (User) type so I can
access the Centers property. This method works fine on my development
machine with the VS 2005 web server.

However, after deploying to the Windows 2003 server, I get this exception:

[NullReferenceException: Object reference not set to an instance of an
object.]

That is, it seems that Session["userdata"] is now null. Again, this only
happens on the production IIS server.

So... am I doing something wrong here trying to access Session[] from a
static class? If not, why does it work on VS 2005 but fails on IIS 6?

Thanks in advance,

-Benton
 
M

Mark Rae

I'm having a problem with a method which resides on a static class I have
on my ASP.NET application.

You have to be *really* careful with static classes in ASP.NET:
http://support.microsoft.com/kb/893666
The point here is how I cast Session["userdata"] to the (User) type so I
can access the Centers property. This method works fine on my development
machine with the VS 2005 web server.

Ah yes, but you're the only person connected to it, so the fact that you're
using statics doesn't matter... :)
 
B

bruce barker

either session is disabled on the server or the server is recycling. if
you use inproc sessions, you should always check for null, and have
recovery code.

-- bruce (sqlwork.com)
 
B

Benton

either session is disabled on the server or the server is recycling. if
you use inproc sessions, you should always check for null, and have
recovery code.

-- bruce (sqlwork.com)

Thanks for the advice. I've overcome this using a different approach. Any
clues why it works on VS 2005 intregrated web server but fails on IIS 6?
Same project, same web.config

Cheers,

-Benton
 

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

Top