5

I'm working on an ASP.NET website with a C# backend. De webserver is running on a remote server, and because I need some data located on the client's pc (for instance: the user id), I wrote a client and server application. So when the user starts the client application, it connects to the webserver but doesn't load the page yet, the webserver launches my server which asks the client for some data. After the client responds, the server has the required data and the webpage loads for the client.

To easily access some properties from the user, I wanted to use the Session variables. But when the client sends its data to the server, the Session variable isn't available yet. When I try to access it I get a NullReferenceException. I believe this is because the Application_AcquireRequestState event isn't fired yet (located in Global.asax.cs). Because I need to use the client's data, I save it in a static class so I can access it easily at any time.

Is there a better solution to this? I thought of waiting for the Application_PostAcquireRequestState event to fire, because I think the Session variable is available at that time. So I could then load the data from the static class into the user's session variable. Is this a good idea, or should I just stick with the current situation (static class)? Because it works, but it doesn't feel like the best way to do this.

Thanks in advance!

Carlito
  • 805
  • 10
  • 20
  • 3
    The static variables in your class will be shared between all users, so this isn't a very good idea. – Matten Jan 05 '12 at 10:47
  • @Matten I was afraid for that but not sure. So should I use the Session variable instead? Because when I need it, it isn't instantiated yet, so I have to temporary save the user data. – Carlito Jan 05 '12 at 10:54
  • @Matten: As far I know, `Static Class` is different from `Static Variable` and `Static Class` is not shared among all users..correct me if I am wrong.. – techBeginner Jan 05 '12 at 11:09
  • 1
    @dotNETBeginner a `static class` only contains `static members`, as it can't be instantiated (http://msdn.microsoft.com/de-de/library/79b3xss3.aspx) – Matten Jan 05 '12 at 11:11

2 Answers2

4

Session state is good enough for per-user-session data.

Don't touch statics, these are scoped per AppDomain in a process. IIS will recycle AppDomains without you knowing, binning your static variables.

Update: for the sake of clarity, the following question/answer explains a situation where Session will be null:

What should I do if the current ASP.NET session is null?

This obviously makes Session state unsuitable if your application falls into one of the aforementioned situations.

Community
  • 1
  • 1
Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187
  • So if I understand correctly, my static variables could be deleted, so I need to use Session? – Carlito Jan 05 '12 at 10:52
  • @carlito Yeah pretty much, plus they aren't per-user. – Adam Houldsworth Jan 05 '12 at 10:53
  • Alright, thanks. But when I need the Session variable, it isn't instantiated yet. So I have to temporary save the user data, until the Session variable is available. Is there a smarter way to do this? – Carlito Jan 05 '12 at 10:56
  • @Carlito Unfortunately I'm not sure why the Session isn't available. It's a simple key-value store that is always "around" whenever processing is happening in the page life-cycle on the server. I've never had an instance where it was null. – Adam Houldsworth Jan 05 '12 at 10:57
  • I read about it in this post: http://stackoverflow.com/questions/1382791/asp-net-what-to-do-if-current-session-is-null/1382811#1382811 "If your code runs before the HttpApplication.AcquireRequestState event", and that's the case because the userdata gets send just before that. Well, thanks for the help, I now know I really shouldn't use the static class but think of a workaround. Any ideas though? :) – Carlito Jan 05 '12 at 11:05
  • @Carlito Interesting link. I'd first ask the question: why do you need user state before it has been initialized? That link suggests there are very few instances where a null Session would occur. Standard per-user stuff to do with a page doesn't get this issue. That said, there are other mechanisms. Use ASP.NET Cache and hold a dictionary of user-value pairs. Not sure how you'd identify a "user" in this instance though. – Adam Houldsworth Jan 05 '12 at 11:08
  • I need the user id to fetch configurationdata for that user from a database. Anyways, I think I can continue now. Thanks, I marked your post as answer. – Carlito Jan 05 '12 at 12:15
1

Can you set up a dummy page that gets just far enough to build the Session, then redirect to another page that fetches the whatever-it-is from the remote server?

sq33G
  • 3,320
  • 1
  • 23
  • 38
  • I've been thinking about this and it could work. I have to change alot though, but I think it is the nicest solution. Thanks for the suggestion! – Carlito Jan 05 '12 at 12:12