Dexterslab
Dexterslab

Reputation: 129

Latency issue caused by one of the ajax calls in a page

I have multiple ajax calls on the page. Lets call them A, B, C for the sake of brevity. "A" takes too long like 2 mins to get the data from server (I am ok with 2 mins). Whereas, "B" & "C" response-time is in milliseconds.

For some reason the AJAX calls are getting queued up after "A". Even though the IE 9 can have 6 concurrent request connections simultanousuly for the same domain. You can see the concurrent connections in my screenshot below.

"A" takes 2 mins then "B" & "C" get into pending state until "A" is complete.

I found "ajax_threads.js" http://cmarshall.net/MySoftware/ajax/Threads/ but I don't know if this would help. The way I understood "ajax_threads.js" is that it just provides multiple connections, that feature is there in modren browsers already. IE 7 used to have 2 connecitons.

first ajax call pending

Here is Javascript:

    $(function () {  $.ajax({
            type: "POST",
            url: "Service1.svc/GetDate",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: true,
            success: function (msg) {
                // Replace the div's content with the page method's return.
                $("#Result1").text(msg.d);
            }
        });


        $.ajax({
            type: "POST",
            url: "Service1.svc/GetDate2",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: true,
            success: function (msg) {
                // Replace the div's content with the page method's return.
                $("#Result2").text(msg.d);
            }
        });   }); 

HTML:

  <input type="button" id="Button1" value="1"/><div id="Result1"></div>
  <br/><input type="button" id="Button2" value="2"/><div id="Result2"></div>

Server Code: (Ajax enabled WCF)

     [ServiceContract(Namespace = "")]
     [AspNetCompatibilityRequirements(RequirementsMode =  
     AspNetCompatibilityRequirementsMode.Allowed)]
     public class Service1
{
    [OperationContract]
    public string GetDate()
    {
        System.Threading.Thread.Sleep(8000);
        return DateTime.Now.ToString();
    }

    [OperationContract]
    public string GetDate2()
    {
        System.Threading.Thread.Sleep(1000);
        return DateTime.Now.ToString();
    }     
}

Upvotes: 0

Views: 850

Answers (2)

Dexterslab
Dexterslab

Reputation: 129

There is no easy way to deal with this issue. I tried several options out there online. But the best solution is to use new feature of ASP.NET "ASP.NET WEBAPI".

Good Luck!

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

I bet that you are using ASP.NET Session on your server. That's standard symptoms when you use Sessions. The reason for that is extremely simple and stems from the design of the ASP.NET Session. It is not thread safe. So what ASP.NET does is simply lock the access to it. The consequence of this is that you cannot have parallel requests to be executed from the same Session. They will simply be queued exactly the way you are observing it.

The best way to solve it is to simply get rid of this dreaded ASP.NET Session. Here's what you could put in your web.config to ensure that no developer on your project ever does the mistake of using it:

<system.web>
    <sessionState mode="Off" />
    ...
</system.web>

Upvotes: 2

Related Questions