1

I have two script tags on page, each containing a document.ready(), and each of them is making an ajax call to a page method.

First one loads the values into the select list. Second one loads the tree into the DOM.

<script>
  $(document).ready(function() {
     $.ajax({
         url: 'PageMethods.aspx/GetTop50',
         async: true,
         success: function(data) {
           //loads the values to the select list 
         }
         //rest of stuff...
     });
  })
</script>

<script>
  $(document).ready(function() {
     $.ajax({
         url: 'Default.aspx/GetTree',
         async: true,
         success: function(data) {
         // loads the tree into DOM
         }
         //rest of stuff...
     });
  })
</script>

Why does my GetTree page method keep executing only AFTER the success callback of the GetTop50? I set the breakpoint to GetTree method serverside, and it is only hit AFTER the select list is loaded.

lucafik
  • 295
  • 1
  • 6
  • 18
  • Assuming the above is just a typo *(why retype? copy and paste!)*, there's nothing in the code above that would serialize the requests, so the issue lies elsewhere -- perhaps the server queues the second request, particularly in debug mode. – T.J. Crowder Dec 16 '14 at 08:32
  • 1
    You're talking about "serverside breakpoint". What exactly do you mean when you're talking about sequentiality on the client side? – hon2a Dec 16 '14 at 08:33
  • Why? Because of sheer luck. The requests could arrive at your server in either order (as can the responses at your client). – Bergi Dec 16 '14 at 08:35
  • @T.J.Crowder ok, didnt pay attention to syntax that much. I thought its maybe something in JS I didnt understand. – lucafik Dec 16 '14 at 08:38
  • @hon2a I am just expecting both breakpoints in server methods to be hit before success client callback functions are executed. – lucafik Dec 16 '14 at 08:38
  • @Bergi Because of sheer luck? I could understand that for the order in which the requests arrive to server, but not that server GetTree function is always hit after the first callback is completed. As if the requests are executed synchronously. – lucafik Dec 16 '14 at 08:40
  • @lucafik That's an incorrect assumption. The requests are sent synchronously and it's up to the server to handle them as it will. It might just as well decide to handle one request first and then start with the second, if it's set up that way. – hon2a Dec 16 '14 at 08:43
  • Maybe your server is not parallelized? Or there is some other arbitrary synchronisation restriction on one of the involved components (it's not the javascript)? – Bergi Dec 16 '14 at 08:43
  • How multiple requests are handled depends entirely on your server environment which you've disclosed nothing about. All we can do is speculate that it depends upon how your server handles multiple requests and it also likely depends upon what each request is doing and how they are written. – jfriend00 Dec 16 '14 at 08:47
  • in a limited environment like a development server for example, it's quite common that AJAX requests are always handled in the same order. when the environment and the server-traffic don't vary too much, then there's one "preferred" way your environment handles things. – low_rents Dec 16 '14 at 08:50
  • then its probably some setting that prevents parallelization on the server. I thought also it may have something to do with the implementation of methods in aspx, rather than asmx... but I guess its not the issue. – lucafik Dec 16 '14 at 08:52

2 Answers2

5

The client will start both ajax calls one after the other so that they are both "in-flight" at the same time. It will then be up to the server which one will complete first and depend upon how the server is configured (will it process multiple requests at once) and will depend upon what each request is doing.

If your server only handles one request at a time or if it blocks on some shared resource such as a database, then it will likely return the first request it received before returning the second request result - though that's just a likely option, certainly not a guaranteed option. For example, if the first request pretty much always takes longer to process than the second and they aren't both contending for the same shared resource, then the second request might finish first and return its results first.

It is also possible that the requests will return in a random order such that sometimes one will return first and sometimes the other will return first. As I said earlier, it all depends upon how the server processes each request and which one it finishes first.


Also, keep in mind that client-side JS is single threaded so when you are sitting at a client-side JS breakpoint somewhere, other JS cannot run until the current JS thread of execution has finished. As for the behavior in breakpoints on the server-side, that all depends upon what the server execution environment is and how it works during a breakpoint.


If you want to debug timing-related things, a breakpoint is NOT a reliable way to test things because hitting the breakpoint itself can very easily affect the behavior. Instead, you should use logging with accurate timestamps to study the exact sequence of events.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Thanks. Then I definitely think that some server setting is an issue. I will look into the possible reasons for that. The behavior is not only with breakpoints, I just used it to confirm my assumption after seeing how UI is being rendered sequentially. I know that JS is single threaded, thats why I thought that the issue might be the way I implemented client side code, or maybe the fact that I am using page methods instead of web service methods. – lucafik Dec 16 '14 at 08:55
  • Most of the time the session storage is at fault: if a request contains a session cookie, the session storage is most likely locked for additional requests. There are some clever server implementations that allow the session to be read-only for some requests but I don’t know enough about Asp.net to tell you how to configure this. – Raphael Schweikert Dec 16 '14 at 09:40
  • @RaphaelSchweikert: I also have the same suspicions after finding this two questions: http://stackoverflow.com/questions/5110001/pagemethods-and-session http://stackoverflow.com/questions/5118236/sessions-in-asynchronous-design – lucafik Dec 16 '14 at 09:46
1

I want to thank everyone for the input, especially @jfriend00, and post the exact solution to my problem.

So the problem was that Default.aspx accesses Session, and had EnableSessionState="True" page directive.

When this directive is set, requests are sequentialized on the server side.

I solved it by moving my method into another page, that doesnt use the session state.

lucafik
  • 295
  • 1
  • 6
  • 18