Update 5 Feb 2010 – The latest MVC 2 futures on CodePlex contains the the RenderAction() fix – this sample is no longer needed for a session less controller. Use the futures version.
You have a single client making multiple concurrent requests to the server. The default behavior is that these requests will be serialized; with the session less controller they execute in parallel. For example, the client might make a single long-running request, but the developer wants other shorter requests (such as AJAX requests) from the client not to be blocked. Normally, these later requests would be blocked from running until the first request completes due to a lock being taken on the user’s session. The goal of the APIs being introduced here is to allow the developer to specify on a per-controller level what type of session will be required for the actions within that controller. A complete sample download is available on a link at the bottom of this page. The image below shows the session less controller sample rendered with FireFox.
When I first tried "Reload All Tabs" on the sample using FireFox, each tab was updated serially. It turns out FireFox and other browsers (but not IE8) automatically serialize requests to the same URL. To enable the tabs to refresh in parallel, request the same URL but append a unique query string to each tab. The sample shows the dramatic improvement in refreshing all tabs with session disabled over the session enabled controller.
<httpModules> <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add name="MvcDynamicSessionModule" type="Microsoft.Web.Mvc.MvcDynamicSessionModule, Microsoft.Web.Mvc, Version=2.0.0.0"/> </httpModules>
<modules runAllManagedModulesForAllRequests="true"> <remove name="ScriptModule"/> <remove name="UrlRoutingModule"/> <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add name="MvcDynamicSessionModule" type="Microsoft.Web.Mvc.MvcDynamicSessionModule, Microsoft.Web.Mvc, Version=2.0.0.0"/> </modules>
Note that this module must be declared after the Routing module. If using ASP.NET 4, one or both of these <modules> elements might be empty and will need to be created.
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RegisterRoutes(RouteTable.Routes); ControllerBuilder.Current.SetControllerFactory(new MvcDynamicSessionControllerFactory()); }
The ControllerSessionState attribute specifies the session behavior for a controller. The following snippet demonstrates disabling session state on a controller:
[ControllerSessionState(ControllerSessionState.Disabled)] public class SessionStateDisabledController : Controller {
The ControllerSessionState enumeration is similar to the SessionStateBehavior enumeration introduced in ASP.NET 4. It contains four values:
Credits: Levi wrote the samples, futures code and sketched out the usage.