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?
-
Store Data in a Database (such as SQL Server).
-
ASP.NET session state can be configured to persist to a database.
-
You could use the Profile Provider with a SQL database as your backing store.
See this MSDN Article
Batuta : Care to show some articles or examples? Thanks. -
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.
Batuta : Would you post more examples on this? An article perhaps? Thanks. -
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
Batuta : 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?Josh : 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.Batuta : 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?Josh : 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. -
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.
Batuta : 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.Josh Stodola : Yes, to all questions. The link provided confirms that. You can set the session timeout value (in minutes) in your web.config file. -
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.
1 comments:
This is a nice article..
Its easy to understand ..
And this article is using to learn something about it..
c#, dot.net, php tutorial, Ms sql server
Thanks a lot..!
ri80
Post a Comment