Asynchronous web services


J

jez

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
 
Ad

Advertisements

D

Dan Rogers

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?= <[email protected]>
Subject: Asynchronous web services
Date: Tue, 15 Feb 2005 06:55:03 -0800
Lines: 30
Message-ID: <[email protected]>
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
 
Ad

Advertisements

J

jez

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 said:
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?= <[email protected]>
Subject: Asynchronous web services
Date: Tue, 15 Feb 2005 06:55:03 -0800
Lines: 30
Message-ID: <[email protected]>
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top