Compress session state in ASP.Net 4.0

Hello folks,

In this post I would like to talk about a new feature of ASP.NET 4.0 -  easy state compression.

When we create applications the user must feel that whenever he interacts with the website, he actually interacts with something that can be safely described as an application.

What I mean by this is that is that during a postback the whole page is re-created and is sent back to the client in a fraction of a second. The server has no idea what the user does with the page.

If we had to work on that basis then we would not have an application but a series of disconnected web pages with no use at all.

In order to overcome this deficit, we do have various session state mechanisms that are well documented elsewhere on the internet.

ASP.NET session state can identify requests from the same user during a specified time interval and gives us a way to preserve variable values for the duration of that session-time interval.

Here come the obvious question. Where are all those variables stored?

ASP.NET session state supports several different storage options for session data. One of them is SQL Server.

All the variables data persist to a SQL Server database rather than being stored in memory.

We must create the tables and stored procedures that ASP.NET will look for on the SQL Server. Have a look here for more details on how to accomplish that.

So what happens is that Session state is serialised into bytes and stored in those sql server tables.

Now we must add some configuration to our application. and how do we do that?

We do add some lines in the web.config file.Have a look

<sessionState allowCustomSqlDatabase="true" sqlConnectionString="data source=local;Initial Catalog=sessionstatedb" compressionEnabled="true" />

The important setting that was added in ASP.Net 4.0 is the last setting, compressionEnabled="true".

This means that all the session data that is transferred between the client and the server can now be compressed, resulting in better performance.

Hope it helps!!!


  • That is a very good feature. I can't wait to move to framework 4.0. We have our own custom logic to do that in one of our framework 2.0 application.

    "This means that all the session data that is transferred between the client and server can now be compressed, resulting in better performance.

    Comment about your above statement.

    Session data always stay on server (web server/db server or state server). I believe in this case you meant data transfer between web server and sql server.


  • nice to know the new option
    any other prblm why it is not a default setting(Y we need to explicitly define this)

  • Compress session state in asp net 4 0.. Dandy :)

Comments have been disabled for this content.