3

What does the WCF framework do with the AsyncState and AsyncCallback in the following operation (service-side) implementation?

IAsyncResult BeginWork(<<Service call params>>, AsyncCallback callback, object state);

I am trying to understand

  1. Who are populating these?
  2. What are their purposes?
  3. Related to (2), what are the best practices in using these?
Alwyn
  • 8,079
  • 12
  • 59
  • 107
  • [read this](http://stackoverflow.com/questions/1047662/what-is-asynccallback) – Manish Mishra Apr 02 '13 at 06:51
  • Does not answer the question. Please re-read. Which part of WCF framework is making use of them. And that doesn't even touch on what the `object state` parameter does. – Alwyn Apr 02 '13 at 06:54

2 Answers2

5

There are a couple of design patterns in .NET for exposing asynchronous methods.

This pattern with BeginX and EndX methods is called the Asynchronous Programming Model

In this pattern, you can always pass these two extra parameters:

1) AsyncCallback callback is a delegate which is called when the operation completes.

2) object state is any object you want to associate with the request.

The caller can provide these two parameters, or leave them null, depending on the use case.

See MSDN: Asynchronous Programming Model (APM)


ADDENDUM:

In the case of an async WCF service implementation, WCF itself is the "client" and populates the two extra parameters.

Here's the workflow:

1) request comes from client.
2) WCF constructs an AsyncCallback and a state object
3) WCF calls your BeginX method and passes the parameters
4) In your method, you construct an IAsyncResult object using WCF's parameters
5) You initiate some background work and return your IAsyncResult object
6) WCF waits for its AsyncCallback to be called
7) WCF calls your EndX method and returns the response to the web service client.

The client doesn't know anything about this and may call this web service using either client-side synchronous or asynchronous calls.

Nick Butler
  • 24,045
  • 4
  • 49
  • 70
  • Nicholas, in WCF though, these methods are at the service boundaries. I somehow doubt the client can pass a delegate remotely to the service. Back to the question, who's populating these in WCF context? I apologize if that wasn't clear enough. – Alwyn Apr 02 '13 at 14:58
  • What do you mean ? The client passes a callback to service, when Service finish executing the EndOperation, the callback is invoked on client side. – ilansch Apr 02 '13 at 15:10
  • In the case of async WCF services, WCF is the client and populates them so it can detect when your async service method has completed. – Nick Butler Apr 02 '13 at 15:17
  • @NicholasButler +1. Before I mark this as an answer, what does WCF use the object state for? It should know which request it's handling, because it has both the IAsyncResult and AsyncCallback, why then does it need the object state? Can it not track the request using the hashcode of those 2 delegates? – Alwyn Apr 02 '13 at 17:07
  • I don't know what WCF uses the object state for - I can't remember ever looking! But it's an implementation detail of WCF, so it's best not to rely an any specific behaviour anyway. – Nick Butler Apr 02 '13 at 17:37
2

If you develop async calls with WCF read this, very helpful, the best article i read on this subject with good sample code.
http://www.danrigsby.com/blog/index.php/2008/03/26/async-operations-in-wcf-iasyncresult-model-server-side/

Edit:
Client:

Service.BeginInitialize(data, new AsyncCallback(OnEndInitialize), null);  

Client contains method:

void OnEndInitialize(IAsyncResult asyncResult)
     {

Service will receive the "data" object, do whatever it needs, and then invokes the OnEndInitialize method of the client.

Regarding the object state you ask about, when doing async operation using APM pattern, the service has 2 operations, Begin and End, when he starts, it is with the BeginOperation, when the BeginOperation completes - then the client receives an IAsyncResult object, that object can contain the objectstate. This is used for progress indication, i will consider the object as ping-pong object. When the service finish the operation it calls EndOperation on service side, this calls the callback on the client side, and then client can again check the state object inside the IAsyncResult.
This is the reason BeginOperation and EndOperation contain IAsyncResult param/return.

ilansch
  • 4,784
  • 7
  • 47
  • 96
  • yup, i was struggling with this last week and that link saved me alot of time. too bad it does not talk about returning datacontracts – ilansch Apr 02 '13 at 15:22
  • link was down: https://web.archive.org/web/20130329091820/http://www.danrigsby.com/blog/index.php/2008/03/18/async-operations-in-wcf-event-based-model – juFo Mar 17 '15 at 14:50