4

In C#, how do I make an async call to a WCF Web Service? I have a Service Reference set up to generate async. I also modified my calls to use { WebServiceObject.Begin* () } but there are two parameters at the end, 'AsyncCallback' and 'object asyncState'. What are these and how do I use them?

Thanks,

Phillip
  • 430
  • 1
  • 5
  • 13

3 Answers3

4

You might also want to look at Async without the Pain

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
3

See MSDN here; http://msdn.microsoft.com/en-us/library/system.asynccallback.aspx

here; http://msdn.microsoft.com/en-us/library/ms228969.aspx

here; http://msdn.microsoft.com/en-us/library/ms228975.aspx

and here; http://msdn.microsoft.com/en-us/library/86wf6409.aspx

Basicly in Begin* you set an callback, that callback is called when the operation has completed. There you call End* to retrieve the appropriate data.

thijs
  • 3,445
  • 1
  • 27
  • 46
  • 1
    None of these links are specific to WCF – C.J. Sep 02 '11 at 16:03
  • Have a look at this related StackOverflow question: http://stackoverflow.com/questions/18302560/using-client-side-task-based-operations-with-wcffacility-in-castle-windsor. – Terry Coatta Feb 24 '14 at 20:05
2

Callback is called when operation is completed, so you can call End* and grab return value or exception if any. asyncState is just a value for matching in callback if you use same callback method in several places. Here is a description of Async design pattern - http://msdn.microsoft.com/en-us/library/aa719595(VS.71).aspx

If you are creating GUI application, consider using another version - *Async method which is generated for each operation as well. It provides thread synchronization.

Ihar Voitka
  • 549
  • 3
  • 8