Mark Robinson
Mark Robinson

Reputation: 13278

jQuery Ajax - Running multiple requests against ASP.NET MVC

I have a 'tabbed' style web page where once the page loads each tab fires off an Ajax request for their content (using jQuery). The backend is ASP.NET MVC running in Visual Studio so just with the normal 'ASP.NET Development Server' servicing the requests. I am experiencing the following issues:

1) The requests are processed by the web server one at a time. The second request is not dealt with until the first completes. Is this the 'ASP.NET Development Server' running in some single threaded mode? Can I make it multithreaded (and should I make it multithreaded or would this open up a world of pain?)

2) Long running requests kill off subsequent requests. Given that the requests are being serviced one at a time even though I am firing off the ajax request all at once, why do some of the requests fail to even arrive at the web server? This happens once a long request has been serviced. For example:

I have tried increasing the ajax timeout but with no success.


Update: I have found a bug in my code that was messing up the URL to the final request so it looks like this has nothing to do with the request length and ajax timing out (blushes!). Along the way though I have found the useful ajaxManager plugin which is great for queueing up request if that is what you need (http://www.protofunc.com/scripts/jquery/ajaxManager/). Also, thanks for the valid point about using the ASP.NET Dev Server to test against. It is indeed quite different from IIS.

Upvotes: 2

Views: 3671

Answers (2)

Thamizh
Thamizh

Reputation: 129

When ASP.NET see two or more requests from the same user( by checking their SessionID) and session is marked with both Read and Write, then it will execute the first request and queue all other requests such that only one request will execute from the same user at the same time. So we can say that these requests are serialized. To make your requests execute concurrently, you need to either mark your session as disabled or read only.

For example. How to disable session state in application level:

protected void Application_BeginRequest()
{

   if (Request.Cookies["ASP.NET_SessionId"] != null)

   {

       Request.Cookies.Remove("ASP.NET_SessionId");

   }

}

If you want to allow concurrent requests for few controllers and want to maintain session for other controllers. We can filter out the conditions as below: In below example i am disabling session only for the controller namely "Concurrent". All other controllers will maintain session.

protected void Application_BeginRequest()
    {
    
       if (Request.Url.AbsoluteUri.ToLower().Contains("Concurrent") && Request.Cookies["ASP.NET_SessionId"] != null)
    
       {
    
           Request.Cookies.Remove("ASP.NET_SessionId");
    
       }
    
    }

Upvotes: 0

MrKWatkins
MrKWatkins

Reputation: 2668

With regards to point 1 bear in mind that if you're using session state then the requests will not perform concurrently - ASP.NET will lock the session state and will only process each request that uses it one at a time. (See the section 'Concurrent Requests and Session State' in http://msdn.microsoft.com/en-us/library/ms178581 for more info) Try disabling session state (if you're not using it of course!) and see if that helps.

Upvotes: 5

Related Questions