Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC and Ajax, concurrent requests?

AJAX newbie here!
At the moment in my ASP.NET MVC web app my AJAX requests appear to be getting batched or queued, im not sure.
No requests seem to be getting completed until the previous request has finished.
How do I go about getting the requests to return independantly?
I dont necessarily want someone to give me the answer but maybe some links to good tutorials or resources which could help. Thanks

like image 966
Boob Avatar asked Feb 24 '10 14:02

Boob


People also ask

Can we use Ajax in MVC?

The MVC Framework contains built-in support for unobtrusive Ajax. You can use the helper methods to define your Ajax features without adding a code throughout all the views. This feature in MVC is based on the jQuery features. To enable the unobtrusive AJAX support in the MVC application, open the Web.

How does ASP NET core handle multiple requests?

ASP.NET Core apps should be designed to process many requests simultaneously. Asynchronous APIs allow a small pool of threads to handle thousands of concurrent requests by not waiting on blocking calls. Rather than waiting on a long-running synchronous task to complete, the thread can work on another request.

Why We Need Ajax call in MVC?

It is a client-side script that communicates to and from a server/database without the need for a postback or a complete page refresh. The Ajax speeds up response time. In other words, Ajax is the method of exchanging data with a server, and updating parts of a web page, without reloading the entire page.

Can Ajax work with ASP NET?

AJAX is used to create dynamic web pages that do not require page reloading when any part of the whole web page content or the whole web page content is changed. The server data exchange is asynchronous in nature and AJAX in ASP.net uses multiple technologies like XSLT, XHTML, CSS, JavaScript, etc.


4 Answers

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.

like image 161
Seth Petry-Johnson Avatar answered Oct 21 '22 07:10

Seth Petry-Johnson


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" %>
like image 31
Lachlan Roche Avatar answered Oct 21 '22 07:10

Lachlan Roche


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 
{
}
like image 33
Andy Avatar answered Oct 21 '22 06:10

Andy


Since .NET Framework v3.0 released, you can use "SessionStateBehavior" enum with SessionStateAttribute:

[SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)]
public class MyController : BaseController { }
like image 3
Odrin Avatar answered Oct 21 '22 08:10

Odrin