Important things to know when you store a Session State in SQL Server

Keep following things in mind when you store a session state in SQL Server.

1. Every object which you store in session should be serializable.

2. Try to avoid adding null object in session. It will fail while deserializing that object.

3. If you are using language, please make sure you assign some value to sessionkey variable.

For example:

Dim sessionKey as string
Session(sessionKey) = “someValue”

You can see here that I haven’t assigned any value to my variable sessionKey. Even though it will work fine in In-Process Mode. It will store the value in sql server if you are using SQL Server Session State. Only it will fail during deserilization of that value back from SQL Server. So be sure to assign the value to sessionKey variable. If you are getting an error saying “The session state information is invalid and might be corrupted.” make sure that you have assigned value to sessionkey as I explained here. If you have a webfarm scenario and you are getting this error, you have to check machinekey attribute specified on server level web.config. You have to make sure that this validationkey and decryptionkey should be the same for web server’s web.config.

4. By default, ASP.NET assumes that every page requires session state to be loaded during page initialization and to be flushed after the page has finished rendering. When you are using out-of-process session state, this means two round-trips to the state server (or database server) for each page rendering. You can potentially eliminate many of these round-trips by more carefully designing how each page in your application uses session state. The session manager then determines when session state must be retrieved and stored by querying the current handler’s session state requirements. There are three options for a page (or other handler) with respect to session state. It can express the need to view session state, to view and modify session state, or no session state dependency at all. When writing ASP.NET pages, you express this preference through the EnableSessionState attribute of the Page directive. This attribute defaults to true, which means that session state will be retrieved and saved with each request handled by that page. If you know that a page will only read from session state and not modify it, you can save a round-trip by settingEnableSessionState to readonly. Furthermore, if you know that a page will never use session state, you can set EnableSessionState to false. Internally, this flag determines which of the tagging interfaces your Page class will derive from (if any). These tagging interfaces are queried by the session manager to determine how to manage session state on behalf of a given page.

Hope this will help you!

Posted in Microsoft Technology Tagged with:
0 comments on “Important things to know when you store a Session State in SQL Server
1 Pings/Trackbacks for "Important things to know when you store a Session State in SQL Server"
  1. […] Important things to know when you store a Session State in SQL Server […]