Is it possible to call a web service asynchronously from javascript with ASP.NET AJAX?

Last post 10-30-2008 11:39 AM by gt1329a. 5 replies.

Sort Posts:

  • Is it possible to call a web service asynchronously from javascript with ASP.NET AJAX?

    10-29-2008, 9:24 PM
    • Member
      point Member
    • Duffdevice80
    • Member since 10-30-2008, 1:17 AM
    • Posts 3

    Hello,

    I have a web service that I would like to call asynchronously from javascript. I've found documentation indicating that this would be possible to do from managed code, using BeginFoo() and EndFoo() signatures. But it seems that ScriptManager only exposes proxies for the synchronous version of the web method (i.e. Foo() in this case).

     Is there any way to call a webservice from javascript such that I can process the request asynchronously on the web server side? Note that I am already making the call using an asynchronous pattern on the browser side (i.e. by supplying a callback). But I would like the web server side to also use an asynchronous pattern, since it is potentially doing a lot of work before returning.

     Thanks! -Duff

     

  • Re: Is it possible to call a web service asynchronously from javascript with ASP.NET AJAX?

    10-29-2008, 9:34 PM
    • All-Star
      36,330 point All-Star
    • rtpHarry
    • Member since 10-01-2006, 12:51 PM
    • Lincoln, England
    • Posts 5,849

     I just watched this video about half an hour ago that explains how to do this:

  • Re: Is it possible to call a web service asynchronously from javascript with ASP.NET AJAX?

    10-30-2008, 2:56 AM
    • Member
      point Member
    • Duffdevice80
    • Member since 10-30-2008, 1:17 AM
    • Posts 3

     Thanks for the reply. I watched the video you linked to, but this isn't what I need. My client-side script is already invoking the web service asynchronously, that part is working. What I need is to be able to handle this request on the web server side in an asynchronous manner as well.

     Say I define my web service method something like this:

    [WebMethod]
    public void DoWork()
    {
       DoSomethingThatBlocksForAWhile();
    }
    


    I would like to be able to make a call from javascript that looks like:

    var asyncResult = MyWebService.BeginDoWork();
    WaitUntilDone( asyncResult );  // pseudocode
    MyWebService.EndDoWork();
    

      

    This pattern works when calling a web service from managed code (because both asynchronous and synchronous method signatures are generated when the web service proxy is created). But I can't figure out how to use the same asynchronous calling pattern when calling a web service from javascript. It seems that the script manager proxy JS code only has the synchronous entry points by default. Any way to get this to work?

     Thanks! -Duff

     

  • Re: Is it possible to call a web service asynchronously from javascript with ASP.NET AJAX?

    10-30-2008, 8:25 AM
    • Star
      13,617 point Star
    • gt1329a
    • Member since 06-23-2002, 8:53 PM
    • Atlanta
    • Posts 2,249
    • ASPInsiders
      TrustedFriends-MVPs

    If I understand what you're trying to do, that isn't possible with a single request/response.

    Encosia - ASP.NET, jQuery, AJAX, and more.

    Latest article: Emulate ASP.NET validation groups with jQuery validation
  • Re: Is it possible to call a web service asynchronously from javascript with ASP.NET AJAX?

    10-30-2008, 11:33 AM
    • Member
      point Member
    • Duffdevice80
    • Member since 10-30-2008, 1:17 AM
    • Posts 3

     Thanks for the reply. I was afraid that might be the case. Just out of curiousity, could you outline how I might do it with more than one request/response? I don't understand how this could be accomplished, short of polling repeatedly for the done condition.

     -Duff

  • Re: Is it possible to call a web service asynchronously from javascript with ASP.NET AJAX?

    10-30-2008, 11:39 AM
    Answer
    • Star
      13,617 point Star
    • gt1329a
    • Member since 06-23-2002, 8:53 PM
    • Atlanta
    • Posts 2,249
    • ASPInsiders
      TrustedFriends-MVPs

    Polling is one way.  As long as you're using a web service (and not polling too frequently), polling isn't a terrible solution.  I'm not a big fan of the functionality tradeoff you have to make to balance performance and polling resolution though.

    Another way is to use Comet to push updates.  This is a basic example of doing that: http://encosia.com/2007/10/03/easy-incremental-status-updates-for-long-requests/

    It's important to keep in mind that neither technique scales very well though.

    Encosia - ASP.NET, jQuery, AJAX, and more.

    Latest article: Emulate ASP.NET validation groups with jQuery validation
Page 1 of 1 (6 items)