Asynchronous web services

Discussion in 'ASP .Net Web Services' started by jez, Feb 15, 2005.

  1. jez

    jez Guest

    I've been trying for the past two days to get an asynchronous web service to
    work. After understanding (thanks to an online tutorial and the Microsoft Web
    Services book) all the concepts (call back, async result etc...), I still
    haven't managed to get it to work. I am able however to use a synchronous web
    service.

    Here's the code that I have on the client. The webservice takes a string and
    returns a string.

    static void Main(string[] args) {
    jezWS.stoproService ws = new jezWS.stoproService();
    AsyncCallback cb = new AsyncCallback (Class1.ReportCallBack);
    IAsyncResult ar = ws.BegingetPhone("name", cb, ws);
    }

    public static void ReportCallBack (IAsyncResult ar) {
    Console.WriteLine("*** Got response from Web Service ***");
    stoproService ws = (stoproService) ar.AsyncState;
    Console.WriteLine(ws.EndgetPhone(ar));
    }

    The code compiles okay, but after the third line in the Main method it
    simply exits. If however, I put the third line of the ReportCallBack method
    in the main method (i.e. the one with EndgetPhone), I do get a response from
    the web service but it's just not asynchronous! I just want to be able to
    show for instance text on the Console while waiting for a response.

    Thanks so much for the help,

    jez
    jez, Feb 15, 2005
    #1
    1. Advertising

  2. jez

    Dan Rogers Guest

    The code exits because it reaches the end of main. There's no magic here -
    the code works like it always did. When main runs out, the program will
    terminate. What you need to do is put a wait loop into your main. This
    would be the worker thread, or whatever does the text display you spoke of
    while the service is being invoked in the background. FYI, the service
    isn't async, and neither is your call. This begin/end behavior simply
    spins up a background thread that makes the synchronous call for you, and
    then calls your registered callback method when the response is received.
    Your program has to exist (e.g not have exited) for this to work.

    --------------------
    >Thread-Topic: Asynchronous web services
    >thread-index: AcUTblQ87n6jW28vRRCc14ykFnMyWQ==
    >X-WBNR-Posting-Host: 84.9.86.199
    >From: =?Utf-8?B?amV6?= <>
    >Subject: Asynchronous web services
    >Date: Tue, 15 Feb 2005 06:55:03 -0800
    >Lines: 30
    >Message-ID: <>
    >MIME-Version: 1.0
    >Content-Type: text/plain;
    > charset="Utf-8"
    >Content-Transfer-Encoding: 7bit
    >X-Newsreader: Microsoft CDO for Windows 2000
    >Content-Class: urn:content-classes:message
    >Importance: normal
    >Priority: normal
    >X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
    >Newsgroups: microsoft.public.dotnet.framework.aspnet.webservices
    >NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.1.29
    >Path: TK2MSFTNGXA01.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTNGXA03.phx.gbl
    >Xref: TK2MSFTNGXA01.phx.gbl

    microsoft.public.dotnet.framework.aspnet.webservices:5535
    >X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webservices
    >
    >I've been trying for the past two days to get an asynchronous web service

    to
    >work. After understanding (thanks to an online tutorial and the Microsoft

    Web
    >Services book) all the concepts (call back, async result etc...), I still
    >haven't managed to get it to work. I am able however to use a synchronous

    web
    >service.
    >
    >Here's the code that I have on the client. The webservice takes a string

    and
    >returns a string.
    >
    >static void Main(string[] args) {
    >jezWS.stoproService ws = new jezWS.stoproService();
    >AsyncCallback cb = new AsyncCallback (Class1.ReportCallBack);
    >IAsyncResult ar = ws.BegingetPhone("name", cb, ws);
    >}
    >
    >public static void ReportCallBack (IAsyncResult ar) {
    >Console.WriteLine("*** Got response from Web Service ***");
    >stoproService ws = (stoproService) ar.AsyncState;
    >Console.WriteLine(ws.EndgetPhone(ar));
    >}
    >
    >The code compiles okay, but after the third line in the Main method it
    >simply exits. If however, I put the third line of the ReportCallBack

    method
    >in the main method (i.e. the one with EndgetPhone), I do get a response

    from
    >the web service but it's just not asynchronous! I just want to be able to
    >show for instance text on the Console while waiting for a response.
    >
    >Thanks so much for the help,
    >
    >jez
    >
    Dan Rogers, Feb 15, 2005
    #2
    1. Advertising

  3. jez

    jez Guest

    Thanks a lot for the explanation. I created a windows app to test the the
    behavior (as it does not "simply" exit at the end of the main method) and
    could indeed see the correct "asynchronous" behavior taking place. Thank you
    for the clarification.

    jez

    "Dan Rogers" wrote:

    > The code exits because it reaches the end of main. There's no magic here -
    > the code works like it always did. When main runs out, the program will
    > terminate. What you need to do is put a wait loop into your main. This
    > would be the worker thread, or whatever does the text display you spoke of
    > while the service is being invoked in the background. FYI, the service
    > isn't async, and neither is your call. This begin/end behavior simply
    > spins up a background thread that makes the synchronous call for you, and
    > then calls your registered callback method when the response is received.
    > Your program has to exist (e.g not have exited) for this to work.
    >
    > --------------------
    > >Thread-Topic: Asynchronous web services
    > >thread-index: AcUTblQ87n6jW28vRRCc14ykFnMyWQ==
    > >X-WBNR-Posting-Host: 84.9.86.199
    > >From: =?Utf-8?B?amV6?= <>
    > >Subject: Asynchronous web services
    > >Date: Tue, 15 Feb 2005 06:55:03 -0800
    > >Lines: 30
    > >Message-ID: <>
    > >MIME-Version: 1.0
    > >Content-Type: text/plain;
    > > charset="Utf-8"
    > >Content-Transfer-Encoding: 7bit
    > >X-Newsreader: Microsoft CDO for Windows 2000
    > >Content-Class: urn:content-classes:message
    > >Importance: normal
    > >Priority: normal
    > >X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
    > >Newsgroups: microsoft.public.dotnet.framework.aspnet.webservices
    > >NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.1.29
    > >Path: TK2MSFTNGXA01.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTNGXA03.phx.gbl
    > >Xref: TK2MSFTNGXA01.phx.gbl

    > microsoft.public.dotnet.framework.aspnet.webservices:5535
    > >X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webservices
    > >
    > >I've been trying for the past two days to get an asynchronous web service

    > to
    > >work. After understanding (thanks to an online tutorial and the Microsoft

    > Web
    > >Services book) all the concepts (call back, async result etc...), I still
    > >haven't managed to get it to work. I am able however to use a synchronous

    > web
    > >service.
    > >
    > >Here's the code that I have on the client. The webservice takes a string

    > and
    > >returns a string.
    > >
    > >static void Main(string[] args) {
    > >jezWS.stoproService ws = new jezWS.stoproService();
    > >AsyncCallback cb = new AsyncCallback (Class1.ReportCallBack);
    > >IAsyncResult ar = ws.BegingetPhone("name", cb, ws);
    > >}
    > >
    > >public static void ReportCallBack (IAsyncResult ar) {
    > >Console.WriteLine("*** Got response from Web Service ***");
    > >stoproService ws = (stoproService) ar.AsyncState;
    > >Console.WriteLine(ws.EndgetPhone(ar));
    > >}
    > >
    > >The code compiles okay, but after the third line in the Main method it
    > >simply exits. If however, I put the third line of the ReportCallBack

    > method
    > >in the main method (i.e. the one with EndgetPhone), I do get a response

    > from
    > >the web service but it's just not asynchronous! I just want to be able to
    > >show for instance text on the Console while waiting for a response.
    > >
    > >Thanks so much for the help,
    > >
    > >jez
    > >

    >
    >
    jez, Feb 16, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gary Howlett

    Asynchronous Web Services

    Gary Howlett, Aug 19, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    420
    Natty Gur
    Aug 19, 2003
  2. Dave
    Replies:
    0
    Views:
    124
  3. Bill Zack
    Replies:
    4
    Views:
    134
  4. David Cornelson

    Asynchronous Web Services called from ASP.NET

    David Cornelson, Dec 8, 2003, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    146
    Ágúst
    Dec 9, 2003
  5. sing

    Asynchronous Web Services

    sing, Feb 16, 2004, in forum: ASP .Net Web Services
    Replies:
    2
    Views:
    169
    Sami Vaaraniemi
    Feb 17, 2004
Loading...

Share This Page