Nikolaos Kantzelis ASP.Net Blog

This blog will focus on ASP.NET Framework

Sponsors

About Me

Great Blogs

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 web-asp.net 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!!!


Comments

nikshukla said:

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.

Regards,

Nik

# March 30, 2010 9:39 AM

karthic_85 said:

nice to know the new option

any other prblm why it is not a default setting(Y we need to explicitly define this)

# April 12, 2010 11:11 AM

weblogs.asp.net said:

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

# May 31, 2011 12:06 PM