Bit more detail on how WCF LOB Adapter SDK works with WCF IChannelFactory and IChannelListener

Here is a response in relation to a question asked in this post

Q. It seems WCF LOB adapters works different from normal WCF service as there is no WCF Service hosted. Do we only use channelfactory to create custom channel for send wcf message to LOB without channellisterner? I just wonder how WCF LOB adapter run time works behind scene.

WCF Service is hosted using a class called ServiceHost.  The ServiceHost encapsulates the functionality regarding bindings, channels, dispatchers and listeners. 

WCF LOB Adapter is surfaced to the consumer as a WCF Binding, where the binding is used to create the channel stack. This binding can be considered a peer to other predefined WCF bindings such as BasicHttpBinding, WsHttpBinding, NetTcpBinding, etc and can be set via app.config or in code by the client application when calling a service.  This binding contains an ordered set of binding elements, with adapter being the key binding element that derives from TransportBindingElement class.  In an outbound scenario, the WCF LOB Adapter SDK runtime uses channel factory to create the adapter (i.e. transport channel).  In an inbound scenario, the WCF LOB Adapter SDK runtime uses channel listeners that listen for incoming channels in a service application. 

When a .NET application set the endpoint information with the WCF LOB Adapter binding, WCF under-the-covers calls

When an application uses the WCF LOB Adapter in an outbound scenario, the WCF LOB Adapter SDK runtime interprets the BindingElement::BuildChannelFactory to build a Channel Factory for a specific type of channel.  When ChannelFactory::OnCreateChannel call is made by WCF, WCF LOB Adapter SDK runtime performs some of the following steps –

  • Checks the type of channel (IOutputChannel, IOutputSesionChannel, IRequestChannel, IRequestSessionChannel) and creates appropriate adapter send channel
  • Create connection pool
  • Create metadata cache

 

Then, on channel’s OnOpen() method, WCF LOB Adapter SDK runtime creates the connection (or uses the connection from pool), builds appropriate outbound handler that implements interface IOutboundHandler and/or IAsyncOutboundHandler and call the Execute/BeginExecute/EndExecute method for message exchange.

Similarly, when an application uses the WCF LOB Adapter in an inbound scenario, the WCF LOB Adapter SDK interprets the BindingElement::BuildChannelListner to accept channels of a specific type from the binding (i.e. the adapter).  The adapter checks for the type of channel (IInputChannel, IInputSessionChannel, IReplyChannel, IReplySessionChannel) and creates appropriate adapter receive channel.   Then on channel’s OnOpen() method, WCF LOB Adapter SDK retrieves the connection from the pool, builds appropriate inbound handler that implements IInboundHandler and/or IAsyncInboundHandler, and invoke the TryReceive()/WaitForMessage() methods to map the lob-specific method calls to WCF messages.   The listening application/service then uses this channel to receive incoming messages.

Check the EchoAdapterSample's TestEchoAdapter_Channel (outbound) and TestEchoAdapter_Channel_Inbound (inbound) projects to see how the channel model is used directly to communicate with the adapter.  Then see how the WCF Service Model sample consumer projects TestEchoAdapter_Proxy (outbound) and TestEchoAdapter_Service_Inbound (inbound) hide the channel related details from the end-user.  The EchoAdapterSample is available from the WCF LOB Adapter SDK install directory under \Documents\Samples.

Let me know if you need additional information.