12 January 2011 MVC3, Sessionless Controller CI Team

imageAt the moment I play with MVC3 RC. A new feature which is the introduction of a SessionState Behavior to, for example, make a controller state-, and sessionless. Unfortunately there wasn´t a really advertising.

How it works, what to keep in mind and why you should use it? Go on reading Zwinkerndes Smiley

Sessionless? My demo application

imageMy demo application consists of two controllers. One of them is "sessionless". Also we have to views for the assignment. Beside the MVC App is an "Empty Website" (except the routing, which is usually leading to the SessionController).





    [ControllerSessionState(SessionStateBehavior.Default)]
    public class SessionController : Controller
    {
        //
        // GET: /Session/

        public ActionResult Index()
        {
            Thread.Sleep(1000);
            ViewModel.Session = this.ControllerContext.HttpContext.Session.SessionID;
            return View(ViewModel);
        }

    }

We are interested in the ControllerSessionState Attribute (Beware: I´ve heard that this will be renamed in the final so you need to look for it again Zwinkerndes Smiley)

Through the attribute controller-state you are able to access the Sessionstates:

image

If you want to turn it off totally than choose "disable". Afterwards the session will be "zero".

What things should I keep in mind?

On the first few: You can´t access the Session. That means, for example, for recognizing the user you use the FormsAuth Cookie and so on. As soon if you try to write something into the "TempData" you are going to receive an Exception "The SessionStateTempDataProvider class requires session state to be enabled". If you try to move a file from controller A to controller B you need a cookie. But there is a CookieStateTempDataProvider used to be in the new MVC Futures.

Why should I use this?

Nice question. I found a nice declaration in a Stackoverflow Post:

The release notes cover this more (you can download them from the download link above). Session state is designed so that only one request from a particular user/session occurs at a time. So if you have a page that has multiple AJAX callbacks happening at once they will be processed in serial fashion on the server. Going session-less means that they would execute in parallel.

Here is another declaration: Concurrent Requests in ASP.NET MVC

Is there a consequence for the performance?

imageI´ve done some experiments with Visual Studio 2010 Test Tools and I tried to run a WebPerformances Test in the controller without the Thread.Sleep with 1000 iterations on sessionless and on the "normal" controller.

Beware: I´ve never tested something with WebTest Tools from Visual Studio 2010 Test Tools. Because of this it´s possible that my results are wrong.

The test runs on my Macbook in a VM. The WebApp is hosted in IIS7 - because of this the results aren´t significant for any performance stories of an IIS.

Results for 1000 repeats (open the site a 1000 times) and NO Thread.Sleep:

Try SessionLess (time in sec.) Session (time in sec.)

1             5,70                                     6,17

2             5,15                                     6,21

3             5,28                                     5,15

4             5,16                                     6,74

5             5,13                                     5,54

6             6,68                                     5,50

7             5,12                                     5,17

8             5,30                                     5,66

9             6,28                                     5,30

10           4,98                                     5,27

On both sites there are some bolters but all in all we can see, that the Sessionless Controller is a bit faster.

I´ve done another test ( a so called "LoadTest") with equal results. By the way: Really interesting tool, I´m sure it´s worth to take a look on it later on. But enough for now.

Result

In view of "big" web applications and scalability this could be an interesting subject for you. But if we talk about a usually site I´m sure you can live without this. To put something into a session is sometimes very comfortable and nice for some applications like for example wizard. That´s what I think about but maybe I´m wrong Zwinkerndes Smiley

[Download Sample] (Beware: Maybe you will need a higher version of Visual Studio to run the Test project)