1

I need to call a 3rd party web service from within a console application using the asynchronous method provided by their API. I'm using the old ASMX web reference way to generate a proxy.

I have a class that does the following operations and I want to create an instance of it, make the call to the service with it, but then I want to wait for the completion of the callback and only then repeat with a new instance, new call, new callback etc.

I do not want to have more than 1 call active at a time. i.e. only 1 instance of the class will exist at a time.

The web service calling code looks like this:

using(ABCWebService service = new ABCWebService())
{
    ...
    service.ExecuteCallAndWaitResultCompleted += service_ExecuteCallAndWaitResultCompleted;
    service.ExecuteCallAndWaitResultAsync(parm1, parm2, .., stateObj);
}

...

The callback looks like this:

void service_ExecuteCallAndWaitResultCompleted(object sender, ExecuteCallAndWaitResultCompletedEventArgs e)
{
    // collect data and then move onto next sequential call
}
mikepoole72
  • 65
  • 2
  • 9
  • What is the return type of the AsyncMethod? You might be able to use one of the [TaskFactory.FromAsync](http://msdn.microsoft.com/en-us/library/dd321472%28v=vs.110%29.aspx) methods to then use the async/await patter. – John Koerner Nov 17 '14 at 17:43
  • 5
    Why even call it asychronously if you want to block? – Stephen Kennedy Nov 17 '14 at 17:45
  • @StephenKennedy he doesn't want to block, he just doesn't want concurrent calls. – Cory Nelson Nov 17 '14 at 17:49
  • 1
    @CoryNelson I think he wants to block. It's a console app which calls a web service, waits for response, does something once responses arrives, and then start again, ad infinitum. Therefore go old school and call the non async client code. Apologies in advance if I misread the situation. – Stephen Kennedy Nov 17 '14 at 17:51
  • Seems like an unnecessary case of async IO. You seem to assume that the server provides async APIs when in fact server and client are independent. You can use both sync and async with *any* server. See http://stackoverflow.com/a/22591516/122718 – usr Nov 17 '14 at 17:56
  • @StephenKennedy I had got totally hung up on the sample code provided to me, assumed it was the only way to request the call and neglected to consider the simplest of options to use the synchronous method. Thanks to all... – mikepoole72 Nov 17 '14 at 19:35
  • No problem Mike I'm glad we got to the bottom of it. – Stephen Kennedy Nov 17 '14 at 19:39

1 Answers1

0

I highly doubt there is an "asynchronous method provided by [the] API". A web method is a web method. It's your call which is asynchronous, that is to say by calling an async function on your client proxy class you tell the Framework to (in basic terms) fire off a request to this resource on another thread and call you back when it has a result. Its your client call which is asynchronous.

In Visual Studio (2012), when you Add Service Reference and then click Advanced and then Add Web Reference to create a service reference based on what VS calls "code based on .NET Framework 2.0 Web Services technology", which I gather is how you've done it, the resultant client class has not just asynchronous methods for each web method but synchronous methods too - one for each web method in the service. These synchronous methods will not return anything or yield control back to the caller until they have the result.

If I have your usage case correct, you want your console app to call the web method, wait for a response, do something once the responses arrives, and then start again. In that case, instead of calling the async method (for example GetPeopleAsync) call the synchronous method (GetPeople) - possibly inside a loop.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
  • Slight complication once I tried to run the synchronous method this morning. The call to the service generates an excel report. The synchronous method returns straight away but the report can take time to generate. With the async method I can guarantee the existence of the report to do IO processing on it and also prevent concurrent executions. How can I block the thread while I wait for completion of the callback? – mikepoole72 Nov 18 '14 at 09:57
  • Ended up using ManualResetEvent as per http://stackoverflow.com/questions/3909063/calling-an-asynchronous-method-serially – mikepoole72 Nov 18 '14 at 10:14