How to tell when AJAX was aborted during a long task (WCF) RRS feed

  • Question

  • I have normal WCF JSON method which is being called via JQuery AJAX from the client browser.

    The method takes a fairly long time to execute. The problem is that if the user navigates to a new page during the method's execution, the new page which also makes use of the WCF service, waits until the previous method returns before it executes the method.

    How can I get around this? I'd ideally like to execute the new methods immediately.

    I’ve already tried aborting the AJAX on the client side, but this does not stop the server from executing its task.

    • Edited by MarzSocks Tuesday, November 12, 2013 12:35 PM
    Tuesday, November 12, 2013 12:31 PM

All replies

  • Hi MarzSocks,

    For web page which invoke WCF service via javascript call, all the calls will be issued asynchronously at background (to server-side service). And the javascript client cannot proactively abort the server-side execution. Generaly, we can use the following pattern to handle long-time service call in javascript.

    1) Use XML http request to invoke service operation and register callback function for success or error result

    2) Set a flag (use a global variable or something) in your code to indicate whether the client-side (certain part on page) is still waiting for the service result or not

    3) Whenever the service operation at server-side finishes, the callback function first check the flag variable. If the variable indicate that the client is no longer waiting for the result (such as the user has navigate to a new page), then the callback function simply ignore response data and return.

    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, November 13, 2013 2:39 AM
  • Hi there,

    Thanks so much for the reply.

    Responding to point 1: So using JQuery’s AJAX method I believe the client is indeed doing an async call (please correct me if I’m wrong , but the JQuery async property is set to true, which makes it async). Secondly, do you have some example code to look at for the implementation of your suggestion?

    Responding to point 2: I guess this is my real question: how do I detect if the client is no longer listening so I can set that global variable? Is there an event I can attach? I’ve already tried creating a background thread which monitors Response.IsClientConnected but this is true even when jqXHR.abort() is called. I know for fact that the abort() is working on the client side because the browser stops showing postback activity (referring to processing icon found in the tab of the browser).

    Much appreciated.

    • Edited by MarzSocks Wednesday, November 13, 2013 6:17 AM
    Wednesday, November 13, 2013 6:16 AM