Skip to main content

Moving Session Data from InProc to State Server or Database And There Problems

Its been a while since i posted , almost 4 months now, was busy with office work.
So in this post I would like to talk about ASP.NET sessions.

Ok...now we all know about ASP.Net sessions, we all know there are few methods which you can use to store session information, one common way and default way is store session state in the memory of the machine in which ASP.NET is running, the other way is to use the ASP.Net state server or to use the default SQL Server provider to store session data, we are not going back to the basics however, we should be careful in choosing where the session data will be stored and what sort of data will be stored.
It's particularly important to know that moving session data from one mechanism to another (in proc to state server)is not just a matter of changing the configuration file.
When using in-proc data is stored in memory, but when storing it in the state server or on a database like SQ Server, these session data needs to be serialized.

Not all data is serializable, like for an example the generic Dictonary or XElement class, so you have to be careful of what you store in the session, if your thinking that in the future you might go for a State server.

It depends on what data is stored in the session, for an example, the best thing for you to do is to store very less data in the session so that you don't use up a lot of server memory for storing user data.

There is also sometimes when you would want to store view State data in the session, hence, the amount of data that goes back forth from the user browser to the server is minimized, here is an MSDN Link on how you can do that.

In the above case, that is where you can do real bad then good, cos' you might be storing data that needs to be persisted on a post back, these might include small data sets to a few custom objects.
These are some of the cases where you can go wrong when you stat storing data in sessions and you are moving from in proc to database or state server...

1) You create an annoynmous type and bind it to a grid, well annoynmous types are not serilizable hence, this will not work if you are using the state server or a database to store session data. You would have to create a wrapper type, and use this to be stored in the session.

2) Any objects that has a property of type XElement, unfortunately this type is not serlizable, hence you would have to use either XmlDocument to store the representation of XElement or take to step explained below on the 3rd point.

3) If you are storing one of your custom types, these have to be marked with [Serilizable] attribute, however there are some cases where you would inherited from a class that is not marked as serilizable, marking your implementation Serilizable wont do any difference, one way to come around this problem maybe to use an XmlSerlizer to serialize the object into XML representation and then store the text in the session (there is a overhead here...)

All the 3 steps mentioned have there overheads, so choose wisely on what data is stored inside session.

P.S Some of these ideas came from Yohan S.

Comments

Popular posts from this blog

Hosting WCF services on IIS or Windows Services?

There came one of those questions from the client whether to use II7 hosting or windows service hosting for WCF services. I tried recollecting a few points and thought of writing it down. WCF applications can be hosted in 2 main ways - In a Windows service - On IIS 7 and above When WCF was first released, IIS 6 did not support hosting WCF applications that support Non-HTTP communication like Net.TCP or Net.MSMQ and developers had to rely on hosting these services on Windows Services. With the release of IIS 7, it was possible to deploy these Non-Http based applications also on IIS 7. Following are the benefits of using IIS 7 to host WCF applications Less development effort Hosting on Windows service, mandates the creating of a Windows service installer project on windows service and writing code to instantiate the service, whereas the service could just be hosted on IIS by creating an application on IIS, no further development is needed, just the service implementa

The maximum nametable character count quota (16384) has been exceeded

Some of our services were growing and the other day it hit the quote, I could not update the service references, nor was I able to run the WCFTest client. An error is diplayed saying " The maximum nametable character count quota (16384) has been exceeded " The problem was with the mex endpoint, where the XML that was sent was too much for the client to handle, this can be fixed by do the following. Just paste the lines below within the configuration section of the devenve.exe.config and the svcutil.exe.config files found at the locations C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE , C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin Restart IIS and you are done. The detailed error that you get is the following : Error: Cannot obtain Metadata from net.tcp://localhost:8731/ Services/SecurityManager/mex If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address. F

ASP.NEt 2.0 Viewstate and good practices

View state is one of the most important features of ASP.NET because it enables stateful programming over a stateless protocol such as HTTP. Used without strict criteria, though, the view state can easily become a burden for pages. Since view state is packed with the page, it increases size of HTTP response and request. Fortunately the overall size of the __VIEWSTATE hidden field (in ASP.NET 2.0) in most cases is as small as half the size of the corresponding field in ASP.NET 1.x. The content of the _VIEWSTATE field (in client side) represent the state of the page when it was last processed on the server. Although sent to the client, the view state doesn't contain any information that should be consumed by the client. In ASP.NET 1.x, if you disable view state of controls, some of them are unable to raise events hence control become unusable. When we bind data to a grid, server encodes and put whole grid in to view state, which will increase size of view state (proportional to the