Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the advantage of using Session-less Controllers in ASP.NET MVC3? [closed]

Can anyone get me a detailed answer on this?

What's the advantage of using Session-less Controllers in ASP.NET MVC?

like image 469
Febin J S Avatar asked Mar 29 '11 06:03

Febin J S


People also ask

What are the advantages of using the ASP NET MVC programming approach over the traditional ASP NET webforms programming?

The ASP.NET MVC framework offers the following advantages: It makes it easier to manage complexity by dividing an application into the model, the view, and the controller. It does not use view state or server-based forms.

What will happen if we are using TempData in controller and we have disabled session?

TempData in MVC uses session state internally to store the data. So when we disable the session state for the controller, it will throw the exception as below.

Which is better TempData or session in MVC?

TempData is ideal for short-lived things like displaying validation errors or other messages that we don't need to persist for longer than a single request. Session is ideal choice when we need to persist data for extended periods of time i.e. it is used to store data which is required throughout user session.


2 Answers

If you have a Session-less Controller then you will be able service simultaneous requests from the same browser instance. Otherwise, ASP.NET will queue up the requests as they are received in order to give them sequential access to the ASP.NET Session object, to avoid deadlock issues or other race conditions.

You can actually have 1 request to a 'Session-ful' Controller and multiple requests to 'Session-less' Controllers simultaneously. This pattern has been useful to me in implementing a long-running process (implemented through a 'Session-ful' AsyncController) with AJAX calls from the client to a 'Session-less' Controller which provides the user with updates as to how far through the long-running process the server is.

like image 184
James Webster Avatar answered Sep 22 '22 02:09

James Webster


In one word: Scalability. If you don't use session at all it means that your application is stateless which is great. In a web farm scenario you just throw another server and you are ready to tackle a new load of your site. You could also use out of process sessions (like SQLServer or StateServer) and the session will be shared between all nodes of the farm but then this state server becomes a sensible single point of failure of the entire site.

There is also another issue with sessions: because session is not thread safe if there are two parallel requests for the same session (think AJAX calls) to a controller action which writes to the session those two requests will simply queue and execute sequentially.

like image 27
Darin Dimitrov Avatar answered Sep 21 '22 02:09

Darin Dimitrov