Dave Burke - Freelance .NET Developer specializing in Online Communities

A freelance .NET Developer

Asynchronous Web Service Calls are swell

Asynchronous Web Service calls are a beautiful thing.  Web services I was executing from a console app I mentioned earlier were timing out and Asynchronous WS calls got me back in the pink.  I sketched out the basic framework in using an asynchronous call below.  I won't provide a play-by-play because there are good references elsewhere.

private static void CoolWebService()
{
    Appname.Component oComponent = new Appname.Component();
    Appname.Server.Class ws = new Appname.Server.Class();
    AsyncCallback cb = new AsyncCallback(THISClass.CoolWebServiceCallback);
    IAsyncResult ar = ws.BeginCoolWebService(cb, ws);
    while (ar.IsCompleted == false)
    {
    // wait
    }
}

public static void CoolWebServiceCallback(IAsyncResult ar)
{
    Appname.Server.Class ws = (Appname.Server.Class) ar.AsyncState;
    ws.EndCoolWebService(ar);
}

Posted: Aug 30 2003, 12:34 AM by daveburke | with 5 comment(s)
Filed under:

Comments

Ali said:

You don't need a while loop. When the background thread completes the operation, the callback is called. The while loop there isn't really doing anything. Also, if the web service calls throws an exception it will be thrown at the EndCoolWebServiceCall, so wrapping the EndCoolWebSericeCall method in a try-catch would be a good idea.
# September 9, 2003 9:10 AM

Zoltan Koszegi said:

I made a sample asynchronous callback on a webservice in C#. It worked fine. Then I tried to practise with the 'ar.IsCompleted' code. When I put it in a 'while' cycle the callback function was not called at all. In the 'while' cycle I put some text to the output window by seconds. It seemed that these messages would be never disappear, because the callback couldn't (or never) completed.
mailto: zoltan_koszegi@qdev.hu
# April 19, 2004 5:33 AM

Dave Burke said:

Zoltan, You're absolutely right, as Ali said, the loop file is unnecessary in the example.
# April 19, 2004 7:41 AM

Andre said:

I also have the same problem with Zoltan.
The ar.IsCompleted code never completed at all. I don't even know where is the problem.
# July 20, 2004 11:21 PM

Dave Burke said:

Andre, It may not be completed because the service doesn't run long enough. I've found that the service has to execute a minimum amount of time to set the IsCompleted property.
# July 20, 2004 11:23 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)