473,320 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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

Jan 23 '07 #1
3 1571
"Benton" <co*******@gmail.comwrote in message
news:51*************@mid.individual.net...
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... :-)
Jan 23 '07 #2
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)

Benton wrote:
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
Jan 23 '07 #3
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

Jan 23 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Scott Wickham | last post by:
I'm having a problem saving session information on one form and retrieving it on a subsequent form...for only one out of a number of users. Actually, I'm not absolutely certain it's a session...
6
by: Willem van Rumpt | last post by:
Hi all, I was wondering: Why isn't it possible to write "static classes". By this I mean a class that will _never_ get instantiated, but will always work on and with other static members of its...
9
by: Joanna Carter \(TeamB\) | last post by:
Following on from the other discussion, I have to just check something out with reference to disposal of resources held in static fields. I have a Persistence Framework that is 'globally...
3
by: Ben | last post by:
Do static classes with static members persist between page hits in asp.net? I'm seeing some very weird behavior and that might explain it. I'm not really sure the best way to test it, so I thought...
9
by: Chuck Cobb | last post by:
I am creating some static classes with static methods and static variables, but I have a question: What is the lifetime of static classes and static variables? Is there any risk that these...
7
by: Brybot | last post by:
Apparently it is not possible for a static class to extend an abstract class? I was wondering how else I might be able to go about my problem here? I have a base class Parent which has a static...
4
by: thomson | last post by:
Hi All, Can any one tell me the demerits of using static classes in Web Application,what is the scope of this kind of class Thanks in Advance Thomson
4
by: JC | last post by:
Suppose an ASP.Net project contains a public static class with public methods and members that are used throughout the application. Of course being static, there is only copy of the class within...
4
by: Rene | last post by:
Hi, I was wondering if anyone could tell me why extension methods must be declared on static classes. I mean, why not allowing them to be declared as static methods in regular instance classes?...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.