Can anyone get me a detailed answer on this?
What's the advantage of using Session-less Controllers in ASP.NET MVC?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With