Menu

Asynchronous Responses

asy proger

AsynWcfLib V2 uses C# 3.0 lambda expressions as callback when a response is received. This allows you to access all data of the sending context when asynchronously receiving the response.
See Test1 and [Implement a client using AsyncWcfLib] for an example.

AsyncWcfLib V3 supports C# async-await. Instead of lambda callback expressions, you can write e.g.

 async void MethodX( WcfMessage request )
 {
     //...local variables 
     WcfReqIdent response = await output.SendReceive (request);
     //...continuaion code after await
 }



Despite looking synchronous, the compiler splits the code in portions before await and after await and runs it asynchronous.
The code after await has access to the sending context in form of local variables of the async method.

When no sending context is available (unrequested notification messages from a service) or the sending context is not important, the response can be handled by a 'default message handler' method.
Each ActorOutput may have its own default message handler delegate passed to the constructor.

The next diagram shows the Dataflow when sending a request message from an ActorOutput and receiving the response from the linked ActorInput. The numbers correspond to the dataflow in the diagram:

  1. An ActorOutput (client) is used to send a request message over the network to the connected ActorInput.
    An ActorOutput<TOC> object may additionally carry a user defined 'OutputContext' object.
    Each ActorOutput carries an ActorPort object containing informations about the connected ActorInput.

  2. After the ActorInput (service) has received the request, it generates and sends a response over the network.
    On the ActorOutput side, the response is deserialized and posted into the message queue of the actors synchronization context, see [Threadsafety and SynchronizationContext].

  3. The sending thread fetches the response from the message queue.
    It creates a WcfReqIdent object containing the response WcfMessage, the ActorPort information of the sending ActorInput
    and some more information about this request/response pair.
    It passes the WcfReqIdent to the sending 'ActorOutput' and 'OutputContext'.
    The response message is optionally handled by a continuation after await, or by the callback lambda expression (see [Understand lambda expressions]).
    As the continuation, lambda expressions have access to locally defined variables of the sending method and to other members of the sending actor.

    There exist an extension method "On<T>" that may be used like a 'switch' statement to select code for different response message types
    (see [Understand extension methods]).

  4. When no matching handler has been found, the default messagehandler is called.
    This handler has been passed as WcfMessageHandler to the constructor of ActorOutput.
    The default messagehandler has access to actor members, to all members of WcfReqIdent as well as to the 'OutputContext' object of the sending ActorOutput.

Client_dataflow.png


Related

Wiki: Home
Wiki: Implement a client using AsyncWcfLib
Wiki: Threadsafety and SynchronizationContext

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.