views:

509

answers:

3

Is there any way to specify that a given Controller or Action uses Session state in a read-only manner? In "old" ASP.NET we used to do something like this:

<%@ Page EnableSessionState="ReadOnly" %>

Is there an ASP.NET MVC equivalent? I'm looking to allow my application to serve multiple requests from the same client simultaneously, and turning off session completely is -not- an option in my case.

+1  A: 

This could be a bit tricky--my understanding is setting up the session state is something that happened at the IHttpHandler level. With custom handlers, you could specify the session state by using marker interfaces, like IReadOnlySessionState to declare that it only needs readonly session state. You could try appending that to your controller and see how it flies. I'd suspect it won't because ASP.NET mvc controllers happen well after the IHttpHandler is wired up, but it is worth a shot.

Another solution might be to wrap the session state variables in your own class, and expose a readonly version to enforce readonly. Accessing it across multiple requests shouldn't be a problem, but you are correct that you can get race conditions and such if you start trying to write to the same session variables from multiple angles.

Wyatt Barnett
The goal isn't to make the variables read-only, the goal is to allow multiple simultaneous requests to MVC pages from a single client. Wrapping up session variables in a read-only wrapper won't help that.
Mark
I guess I'm not quite following you then--why does ReadOnly matter if not to avoid race conditions when there could be simultaneous updates? If all your pages are just reading session data, then having simultaneous requests isn't going to hurt. If all your pages need to update the session simultaneously, then wrapping stuff as ReadOnly isn't going to help.
Wyatt Barnett
Read-only matters because the ASP.NET runtime won't allow multiple requests from the same client unless read-only session is specified. I can't HAVE simultaneous requests unless I specify read-only session, and the question asks how to do that in ASP.NET MVC.
Mark
A: 

What about setting the data you want to be "Read Only" as static in your Model? In this way you could have simultaneous requests to MVC with the same data.

Freddy
Threading issues aren't what I'm trying to solve here, the problem is that ASP.NET won't let one web application handle multiple requests from the same client at the same time unless it thinks that either session state is turned off, or that the particular handler processing the requests won't modify session state. Making fields in the model static won't notify ASP.NET that the session state is read-only.
Mark
+2  A: 

A similar question was asked here: http://stackoverflow.com/questions/1464203/disable-session-state-per-request-in-asp-net-mvc

The accepted answer deals with creating a custom route handler that ultimately skips binding to the session. This doesn't exactly answer your question (which is how to declare multiple actions to use readonly session access), but it seemed relevant enough to mention.

Seth Petry-Johnson
Excellent, thank you.
Mark