views:

416

answers:

7

How Can I persist a User-Specific data for an ASP.Net application.

I tried Session Variable - Not good when the worker process recycles.

I need something that can be accessed GLOBALLY by any class of my application.

Advice most welcome.

I tried to utilize asp.net session State Server but I got some DLLs crashing because they are Unserializable.

Is there any other way to have a persistent variable across the application?

+1  A: 

Store Data in a Database (such as SQL Server).

Mitch Wheat
+4  A: 

ASP.NET session state can be configured to persist to a database.

Here is a tutorial on how to set that up.

Andrew Hare
A: 

You could use the Profile Provider with a SQL database as your backing store.

See this MSDN Article

JoshBerke
Care to show some articles or examples? Thanks.
Batuta
A: 

Theres nothing you can really do about the process recycling. If you use the Cache smartly to retain information in a more global sense but you still have the same worker process limitation.

I try and design my app in a n-tier setup with business entity objects. The factory methods for my objects use the cache kind of like a lazy instantation pattern. If its in the cahce, pull it. If not, put it into the cache for next time.

i.e

MyAppsNameSpace.MyBusinessLayerNameSpace.MyObject.GetObject(objectID)

now when this returns my object, it may be from the cache or may not, if the object is under high usage then it will be probably be cached.

This can be used throughout your entire app and because the caching mechanism is maintained centrally you dont really have to worry about it.

BPAndrew
Would you post more examples on this? An article perhaps? Thanks.
Batuta
+1  A: 

You can change the Session State Server to not be in process which will make it far more stable and also seperate it from the worker process (You'll need to be able to start the Asp.NET State Service on the server if it's not already running)

<sessionState mode="StateServer" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes" cookieless="false" timeout="20"/>

Also if you need to share it across applications in the same domain you should be able to give them the same machine key

Josh
Am actually thinking of using StateServer mode, but I need to know if there is anything SPECIFIC which I need to configure in my IIS Server. Thanks.
Batuta
By the way, I wonder why there is still a time out for the StateServer mode? If the state server mode timed out, then the variable stored in the session is also killed right?
Batuta
Yes, there is still a timeout, but is a) more reliable since it won't reset when the application resets, and b) can be set for much larger values because of it's reliablity. All you should have to do is start/enable the "Asp.NET State Service" on your server.
Josh
How do I tell our server guys to turn / enable the ASP.NEt state service? thanks.
Batuta
And also in your example above, the stateConnectionString value is "tcpip=127.0.0.1:42424"Can I set it to any value, ie. "tcpip=myAppServer:42424" or do I need to specify the actual server name?
Batuta
you can, but for performance reasons you usually don't want the service on a seperate server. I think there's a bit more set up involved that way as well, but I don't recall for sure. You should just be able to ask them to start the "Asp.NET State Service" and they should know what to do from there.
Josh
+1  A: 

You should use Session. You can access session state globally in a class like this...

HttpContext.Current.Session

To avoid losing sessions by the worker process recycling, use StateServer mode.

Josh Stodola
Will the State Server mode ensure that the value I store in a session for a particular user persist for a lengthy amount of time? Does the state server mode behaves like the normal session variable? Each user is also assigned a different session id? thanks.
Batuta
Yes, to all questions. The link provided confirms that. You can set the session timeout value (in minutes) in your web.config file.
Josh Stodola
A: 

If you lose data when the worker process recycles then you should stop using the InProc persistance mode for the Session. Use StateServer or SQL Server. Ultimately you could build your own session persistance module if neither satisfies you.

Andrei Rinea