Session State Module Example

This post will tell  you the example of Default, Disabled and Read-Only session module

Session State :  Default

Controller :

  [SessionState(System.Web.SessionState.SessionStateBehavior.Default)]
   public class TestController
   {
        public ActionResult TestSession()
        {
            HttpContext.Session["TestSession"] = "India";
            return View();
        }

   }

View :

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Test Session</title>
</head>
<body>
         Session value "@Session["TestSession"].ToString()"
</body>
</html>

 

Session State :  Disable

Controller :

  [SessionState(System.Web.SessionState.SessionStateBehavior.Disable)]
   public class TestController
   {
        public ActionResult TestSessionAction()
        {
            HttpContext.Session["TestSession"] = "India";
            return View();
        }

   }

As in disable you cannot neither read nor write session. This code will throw the “Null Reference” exception.

Session State :  Read-Only

Suppose initial value HttpContext.Session[“TestSession”] is “India”.

Controller :

  [SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)]
   public class TestController
   {
        public ActionResult TestUpdateSession()
        {
            HttpContext.Session["TestSession"] = "Pune";
            return View();
        }
        public ActionResult TestReadSession()
        {           
            return View();
        }
    }

 View :

TestUpdateSession

<html>
<head>   
    <title>Test Update Session</title>
</head>
<body>
         Session value "@Session["TestSession"].ToString()"
</body>
</html>

TestReadSession

<html>
<head>   
    <title>Test Read Session</title>
</head>
<body>
         Session value "@Session["TestSession"].ToString()"
</body>
</html>

Scenario 1:

1st request TestController/TestReadSession

o/p : Session value “India”

2nd request TestController/TestUpdateSession

o/p : Session value “Pune”

Scenario 2:

1st request TestController/TestUpdateSession

o/p : Session value “Pune”

2nd request TestController/TestReadSession

o/p : Session value “India”

Conclusion : As I said in my previous post that in read-only session is updated at only request level. See here, 1st request has update the session value but it has not affected the next request.

One can set the session state behavior on the controller i.e Session State Behavior can ONLY be set at Controller level NOT at Action level. There are times where controller is Read-Only and there is/are some action which wants to update the session therefore it want Session behavior to be set as Default. Because of one action we cannot change the behavior of other actions. So one way is to write one NEW controller with session state as Default and move that action in that controller. But this logic is not very much feasible.

So, How to solve this problem??  Always remember one think “Every problem has the solution :)”. Just you need to find out.

Hint: As my site slogan say’s “You Rock, when your basics Rocks”, solution is hidden in basic of C#. OVERIDDING.

Will tell  Solution you in my next post “Session State at Action level“.  🙂

Session State Module

Session are maintained at controller level. In .net Session state is managed by the SessionStateModule class, which calls the session-state store provider to read and write session data to the data store at different times during a request. At the beginning of a request, the SessionStateModule instance retrieves data from the data source by calling the GetItemExclusive method. Exclusive means when one is reading or writing don’t allow other to read/write simultaneously.

There are various types of Session State available:

  1.  Default
    • Whenever session state is not mentioned explicitly, it is treated as default. In Default, whenever any read/write operation is performed on session then application locks the session object i.e. only one request can read/write the session at any given point of time.
  2. Disable
    •  When session state is set to Disable then no request can neither read  nor write the session.  If any request tries to read/write the session then application will throw the “Null Reference”  exception.
  3.  Read-Only
    • As name says Read-only, many will consider that session is Ready-only i.e no request can update(write) the session. But this is wrong. If any request tries to change the session object no error will be thrown, it will simply update the session. So don’t get confused with name.
    • The Read-only follows the reader-writer concept i.e if 1st request is for read(read-only), then session get locked by that request until that request gets completed. But if another request came for read(read-only) in parallel with current request then application will allow that request to read. Which means all the subsequent request for read(read-only) will be granted. If any request came for write (Default) then that write request has to wait until all read request are finished. If write request is in progress then all read has to wait.
    • Also any read-only request can update the session. But the updated session will only be persisted until that request is not finished and will not affect global session value. In simple terms, the updated session value will be local to that request.

For example these mode see my next upcoming post. Leave you comment.