ASP.NET MVC and Ajax, concurrent requests? ASP.NET MVC and Ajax, concurrent requests? ajax ajax

ASP.NET MVC and Ajax, concurrent requests?


I'm expanding on Lachlan Roche's answer, which is correct.

The ASP.NET framework will "single-thread" requests that deal with Session scope (a global resource), to prevent one request from interfering with another. In WebForms I think you can use the Page directive to specify that individual pages don't use Session and therefore don't need to treated synchronously like this.

The problem is that in ASP.NET MVC all requests use Session, because it's used to implement TempData. You can disable session state entirely, as Lachlan Roche pointed out, or you can deal with this on a case-by-case basis.

A possible solution might be to kick off your own background threads to process any long-running code, so that the initial request "completes" as quickly as possible.


ASP.NET will serially process requests on a per-session basis unless sessions are configured as disabled or read only in web.config via the enableSessionState attribute on the pages element.

As this is a page setting, this will not affect MVC controllers and they will still be subject to serial request processing.

Curiously, even with sessions disabled or set to readonly, we can still read and write session data. It seems to only affect the session locking that causes serial request processing.

<system.web>    <pages enableSessionState="ReadOnly"/></system.web>

Pages can also have an enableSessionState property, though this is not relevant to MVC views.

<%@ Page EnableSessionState="True" %>


With the release of ASP.MVC 3 you can now add an attribute to your controllers to mark the Session as readonly, which allows actions to be called concurrently from the same client.

Sessionless Controller Support:

Sessionless Controller is another great new feature in ASP.NET MVC 3. With Sessionless Controller you can easily control your session behavior for controllers. For example, you can make your HomeController's Session as Disabled or ReadOnly, allowing concurrent request execution for single user. For details see Concurrent Requests In ASP.NET MVC and HowTo: Sessionless Controller in MVC3 – what & and why?.

- from this DZone article.

By adding SessionState(SessionStateBehaviour.Disabled) to your controller, the runtime will allow you to invoke multiple actions concurrently from the same browser session.

Unfortunately I don't think there is a way to mark an action so as to only disable the session when that action is called, so if you have a controller that has some actions that require the session and others that do not, you will need to move the ones that do not into a separate controller.

In later versions of ASP MVC you can decorate individual controller classes with the SessionStateAttribute

[System.Web.Mvc.SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)]public class MyController : Controller {}