6

I have a ASP.NET MVC3 project where the main aspx page is doing a dynamic loading for its parts using jQuery ajax. So basically when the site loads, it hits /Home/Index and then within Index (aspx) view, there are several lines of jQuery that make ajax calls (to /Home/PartOne and /Home/PartTwo to populate parts of the page.

So every time the page loads, it basically is doing 3 requests: to get index, to get PartOne, and then PartTwo.

The question: Why is there some kind of "wait" time for the the third request to execute? I thought this was the browser concurrent request limit, but why isn't it executing after the 1st request is done?

When I experimentally put "OutputCache" attribute on "PartTwo", it behaves as expected, that it was executing fast. This hints that the problem is not in IIS, but somewhere after that and before it hits my action method.

Here is a screen shot from Chrome network profiler: Chrome network profiler

Here is a screen shot on the MvcMiniProfiler - look at the 3rd row/value, it is waiting for 500ms before executing my controller's action code. enter image description here

My controller code looks like this. Although I snipped the actual code, but code for PartTwo is very trivial (no long computation, no db calls, etc):

public class HomeController : Controller {
    public ActionResult Index() {
        // do something here
        return View();
    }

    public ActionResult PartOne() {
        // do something here
        return View();
    }

    public ActionResult PartTwo() {
        // do something here
        return View();
    }
}

My javascript:

$(document).ready(function () {
    $.ajax({
        url: "/Home/PartOne",
        cache: false,
        success: function (data, textStatus, request) {
            $("#TestContainerOne").html(data);
        }
    });

    $.ajax({
        url: "/Home/PartTwo",
        cache: false,
        success: function (data, textStatus, request) {
            $("#TestContainerTwo").html(data);
        }
    });
});

My index.aspx:

<h2>Home page</h2>    
<div id="TestContainerOne"></div>
<div id="TestContainerTwo"></div>

PartOne.ascx:

<h2>Part One</h2>

PartTwo.ascx:

<h2>Part Two</h2>

Help?

AgileDan
  • 361
  • 1
  • 2
  • 21
Johannes Setiabudi
  • 2,057
  • 17
  • 29
  • It might have something to do with the number of concurrent requests a browser can have with a specific domain. See the answer from this question: http://stackoverflow.com/questions/561046/how-many-concurrent-ajax-xmlhttprequest-requests-are-allowed-in-popular-browse – Dismissile Mar 08 '12 at 21:40
  • I don't think so. Because there are only 3 requests, so in theory, they should be executed back-to-back - which they are. But the delay is happening on the server side. Look at my note about the experiment using the OutputCache - which suggests that it's not caused by the browser/client limitation. – Johannes Setiabudi Mar 08 '12 at 21:42
  • 1
    Things to try: a) Release build with debug="false" in web.config (I guess I read somewhere that when debug is true there are some things that are cached), b) Try the code on IIS or IIS Express, as I usually find the classic dev server is somewhat flaky some times. – Pedro Mar 08 '12 at 22:54
  • I tried both - similar result. The screen shot is actually from running on a IIS7 on W2K8R2 on release mode (browsing from the server itself). – Johannes Setiabudi Mar 09 '12 at 00:54
  • 2
    Are you using Session in the code that you stripped from your controller actions? Also does the stripped code that you have shown here behaves like that? – Darin Dimitrov Mar 09 '12 at 07:12
  • 1
    Try decorating your controller with read only session state: `[SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)]` and see if there is any improvement. – shizik Mar 09 '12 at 07:25
  • Also on the screen shot from Chrome network profiler I have noticed that the delay is for `combined.js` which is requested twice. Are you returning the partial views properly? – shizik Mar 09 '12 at 07:34
  • You may find this question interesting: http://stackoverflow.com/questions/4428413/why-would-multiple-simultaneous-ajax-calls-to-the-same-asp-net-mvc-action-cause – uvita Mar 09 '12 at 12:42
  • @shizik the combined.js is the requester - the ajax call is being done inside combined.js - so it's not combined.js is being requested twice. – Johannes Setiabudi Mar 09 '12 at 13:47
  • Thanks to @DarinDimitrov about the session state - I may be into something here! It seems to be showing some good progress - I will update after some testing. – Johannes Setiabudi Mar 09 '12 at 13:48
  • Thanks to @shizik about the session state - I may be into something here! It seems to be showing some good progress - I will update after some testing. – Johannes Setiabudi Mar 09 '12 at 13:48
  • @DarinDimitrov (or shizik) Any of you want to write your answer - so I can mark it? – Johannes Setiabudi Mar 09 '12 at 14:31
  • @shizik (or DarinDimitrov) Any of you want to write your answer - so I can mark it? – Johannes Setiabudi Mar 09 '12 at 14:31

1 Answers1

7

You should use read only session state or disable it completely in order to get parallel processing of Ajax requests. By default the server locks the session state for requests coming from the same client, so the requests are executed sequential.

This is done by decorating your controller with the SessionState attribute:

[SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)]
public class HomeController : Controller {
    public ActionResult Index() {
        // do something here
        return View();
    }

    public ActionResult PartOne() {
        // do something here
        return View();
    }

    public ActionResult PartTwo() {
        // do something here
        return View();
    }
}
shizik
  • 910
  • 6
  • 16