State – Part 2 (Session State)

September 29, 2010 § 1 Comment

So, we’ve already discussed View State; let’s continue with a discussion of Session State. While discussing View State, we talked about the fact that View State is stored in hidden fields within the HTML page, and this has both positive and negative effects.

Objects in Session are good for the life of the session, rather than the life of the page (as they are with View State). This is an important distinction, and often drives the decision to use either View State or Session state.

Session objects are stored server side on a per-user basis. Typically, the session is stored In-Process (or within the ASP.NET worker process – either aspnet_wp.exe or w3wp.exe, depending on which version of IIS you are using). There are multiple options for storing Session besides In-Process, but that discussion is an entire post in and of itself.

So to start with, how does session work? Well, assuming your application has Session State enabled (which is the default, although you can disable it), a session id is automatically generated when the user first accesses the site, and that session id is transmitted between the client and the server on every request/response. That session id is also tied to a server-side hashtable, which can be accessed with the Session object.

This has some similarities with the technique that we discussed when talking about View State, wherein you transmit a small identifier between the client and server, and fetch the object from (or save the object to) the database for storage. Using Session streamlines the process, and yields some architectural and performance advantages over that technique.  For instance, this pattern using Session State is perfectly acceptable and performant because only the session id is transmitted to and from the client while the actual object remains on the server.

public partial class Example : System.Web.UI.Page 
    public Customer Customer
            return Session["Customer"];
            Session["Customer"] = value;
The session id can be transmitted to and from the client in one of two ways. You can either transmit the session id using a cookie (which is the default), or you can opt not to use a cookie. There really isn’t a significant difference between the two techniques. With the first, the session id is in a cookie, which is invisible (mostly) to the user. With the second, the session id is injected into the url, which is visible to the user. So, if we navigate Firefox to Visual Studio Magazine, for instance, we can view the individual cookies, and see something like this: Cookies in Firefox

The cookieless version can be preferable, as it is possible to deny cookies with a browser setting, effectively making your site unusable for anyone that does have cookies disabled in their browser. In order to switch to the cookieless version, you simply need to modify your web.config file.

<?xml version="1.0"?>
      <sessionState cookieless="true"/>
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs20 \cf2 public\cf0  \cf2 partial\cf0  \cf2 class\cf0  \cf10 Example\cf0  : System.Web.UI.\cf10 Page\cf0  \par ??\{\par ??    \cf2 public\cf0  Customer Customer\par ??    \{\par ??        \cf2 get\par ??\cf0         \{\par ??            \cf2 return\cf0  Session[\cf13 “Customer”\cf0 ];\par ??        \}\par ??        \cf2 set\par ??\cf0         \{\par ??            Session[\cf13 “Customer”\cf0 ] = \cf2 value\cf0 ;\par ??        \}\par ??    \}\par ??\}}
<div style=”font-family: Courier New; font-size: 10pt; color: black; background: white;”>
<pre style=”margin: 0px;”><span style=”color: blue;”>public</span> <span style=”color: blue;”>partial</span> <span style=”color: blue;”>class</span> <span style=”color: teal;”>Example</span> : System.Web.UI.<span style=”color: teal;”>Page</span> </pre>
<pre style=”margin: 0px;”>{</pre>
<pre style=”margin: 0px;”>&nbsp;&nbsp;&nbsp; <span style=”color: blue;”>public</span> Customer Customer</pre>
<pre style=”margin: 0px;”>&nbsp;&nbsp;&nbsp; {</pre>
<pre style=”margin: 0px;”>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style=”color: blue;”>get</span></pre>
<pre style=”margin: 0px;”>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre>
<pre style=”margin: 0px;”>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style=”color: blue;”>return</span> Session[<span style=”color: maroon;”>”Customer”</span>];</pre>
<pre style=”margin: 0px;”>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style=”margin: 0px;”>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style=”color: blue;”>set</span></pre>
<pre style=”margin: 0px;”>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre>
<pre style=”margin: 0px;”>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Session[<span style=”color: maroon;”>”Customer”</span>] = <span style=”color: blue;”>value</span>;</pre>
<pre style=”margin: 0px;”>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style=”margin: 0px;”>&nbsp;&nbsp;&nbsp; }</pre>
<pre style=”margin: 0px;”>}</pre>

There are, of course, some potential issues with using session state, particularly when in a web garden or web farm environment. In the next article in this series, we’ll discuss this problem further and some ways to mitigate or eliminate those issues.


Tagged: , , ,

§ One Response to State – Part 2 (Session State)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading State – Part 2 (Session State) at Mike Vallotton's Blog.


%d bloggers like this: