How to cancel Asynchronous WebService Call

Last post 08-21-2008 9:04 PM by kyus94. 2 replies.

Sort Posts:

  • How to cancel Asynchronous WebService Call

    08-20-2008, 12:28 PM
    • Member
      13 point Member
    • kyus94
    • Member since 08-01-2006, 5:43 PM
    • Posts 13

    I need to cancel Asynchronous WebService Calls from current page, when user clicks on another item on menu.

    Examples on ajax aps.net website use asp button controls to cancel the existing requests. I have html link as menu item, that calls Javascript function which makes 'webservice method calls'. The problem is html controls dose not fire same Sys.WebForms.PageRequestManager events that asp.net server controls fire.

    I can capture invokingrequest event of WebrequestManager that can give me access to executor but it can only abort current request which is not what I want. I want to execute current request but before that cancel existing requests.

    How to get access to currently executing requests or requests that are queued by XMLHttp and perform abort or any other operation on them. Is it possible at all.

  • Re: How to cancel Asynchronous WebService Call

    08-21-2008, 2:36 PM
    Answer

    Kyus,

    I hope I'm not misunderstanding your problem.  If you need to cancel an webservice call you've made from the client, you can modify the code found here to do it: http://geekswithblogs.net/rashid/archive/2007/07/14/Cancel-a-Web-Service-Call-in-Asp.net-Ajax.aspx

    The only requirement is that instead of using the simplified method for calling web services, you'll have to use a proxy method to do it.  Invoking the proxy method returns a WebRequest object which you can save off to a variable.  When you make additional calls, and want to cancel the previous one, you just need to call the WebRequest to get the executor object and call abort() on it.

    Here's a simple example.  I'm hooking up two buttons to call two different service methods, HelloWorld and GoodByeWorld on WebService1 and WebService2, respectively.  Before invoking GoodbyeWorld, we check to see if there is a service currently running using client script and abort it if it is.  Hopefully the sample makes sense to you:

     

    <asp:ScriptManager ID="ScriptManager1" runat="server">

    <Services>

    <asp:ServiceReference Path="~/WebService1.asmx" />

    <asp:ServiceReference Path="~/WebService2.asmx" />

    </Services>

    </asp:ScriptManager>

    <script language="javascript" type="text/javascript">

    var lastExecutor;

     

    function helloWorld()

    {

    var service = Sys.Net.WebServiceProxy.invoke('WebService1.asmx'

    , 'HelloWorld', false, {}, success, fail, '', -1);

    lastExecutor = service.get_executor();

    }

    function goodbyeWorld()

    {

    if(lastExecutor != null)

    {

    if(lastExecutor.get_started())

    {

    lastExecutor.abort();

    }

    }

    var service2 = WebApplication7.WebService2.GoodbyeWorld(success,fail);

    }

    function success(res)

    {

    alert(res);

    }

    function fail(res)

    {

    alert(
    'fail');

    }

    </script>

    <div>

    <input id="Button1" type="button" value="button" onclick="helloWorld()" />

    <br />

    <input id="Button2" type="button" value="button" onclick="goodbyeWorld()" />

    </div>

    James

    James Ashley, Magenic Technologies
    (james.ashley.magenic@gmail.com)
  • Re: How to cancel Asynchronous WebService Call

    08-21-2008, 9:04 PM
    • Member
      13 point Member
    • kyus94
    • Member since 08-01-2006, 5:43 PM
    • Posts 13

    Thanks a lot ....that helped

Page 1 of 1 (3 items)