Introduction
Essentially, WCF is a communication service framework that allows us to use different transmission protocols and different message encoding formats to interact with different WS-* series standards, all these details are handled by the channel stack. To simplify these processes, two models are provided in WCF: one is the application programming model for developers and the other is the channel model for communication. for developers, it is enough to understand the application programming model without involving the channel model. However, the necessary learning of the channel model allows us to truly understand the concept of "communication" in WCF, understand the entire architecture of WCF, so as to build a more robust WCF Service or expand the WCF framework. In this article, we will have a deep understanding of how the channel model in WCF is designed.
Channel Model Overview
In WCF, a series of interfaces and other types of models are provided, which provide an underlying programming model for message sending and receiving. This model is called the WCF channel model. In the channel model, a very important concept is the channel stack. It is a layered communication stack with one or more message processing channels. The stack contains various types of channels, for processing objects, for example, a transmission channel is placed at the bottom layer of the channel stack, which adapts the channel stack to basic transmission, as shown in Figure 1:
Figure 1
In the channel stack, not only the message transmission mode is provided, but also other functions such as processing the message content or message header, these functions are also placed in the channel stack as channels, and we can even write our own channels and add them to the channel stack.
When a Message is transmitted in the channel stack, it will flow through the communication stack as a Message object. For example, the transmission channel is responsible for converting messages between the sender and receiver, and then the Message will continue to flow through the transmission channel, each channel in the channel stack is responsible for providing a communication function, such as adding information to the message header and encrypting the message body.
Channel Object Model
The channel object model is a set of core interfaces required to implement channels, channel listeners, and channel factories. Some base classes are also provided to help with custom implementation. We can see that the channel model has three most important interfaces: Channel, channel listener, and channel factory. Each channel implements one or more interfaces, called channel shape interfaces or channel shapes. The channel listener is responsible for listening for incoming messages, that is, at the message receiving end, then, these messages are sent to the above layer through the channel created by the channel listener. The channel factory is responsible for creating a channel for sending messages, that is, when the message sender is disabled at the Channel factory, close all channels created by the channel factory.
In the channel model, the most important interface is ICommunicationObject, which defines the core interface of the basic state machine implemented by all communication objects. Custom Channel Communication objects can directly implement ICommunicationObject, as shown in Figure 2:
Figure 2
Only these interfaces in Figure 2 belong to the WCF channel model. In WCF, some basic classes such as CommunicationObject and ChannelFactoryBase are also provided. When a custom channel communication object is implemented, you can also directly inherit from these base classes, but note that these base classes only provide convenience for implementing custom Channel Communication objects. They are not part of the channel model.
The ICommunicationObject interface provides a contract for all communication-oriented objects in WCF. In addition to channels, channel listeners, channel factories, scheduling programs, and service hosts, the Protocol defines the basic state. It includes a set of asynchronous versions used to enable, disable, and abort methods for enabling, enabling, and disabling methods, A group of events that provide status transition notifications and public State attributes used to check the object status are defined as follows:
Public interface ICommunicationObject {// State attribute CommunicationState State {get;} // event EventHandler Closed; event EventHandler Closing; event EventHandler Faulted; event EventHandler Opened; event EventHandler Opening; // method void Abort (); void Close (TimeSpan timeout); void Open (TimeSpan timeout ); // Asynchronous Method IAsyncResult inclose (AsyncCallback callback, object state); IAsyncResult BeginClose (TimeSpan timeout, AsyncCallback callback, object state); IAsyncResult BeginOpen (AsyncCallback callback, object state ); IAsyncResult BeginOpen (TimeSpan timeout, AsyncCallback callback, object state); void EndClose (IAsyncResult result); void EndOpen (IAsyncResult result );}
The initial status of ICommunicationObject is "created". You can configure its attributes. Once an object is in the "opened" status, it can be used to send and receive messages, but its attributes are considered immutable. Once the object is in the "Closing" status, it cannot process new sending or receiving requests, but the existing requests may be completed before the "Closing" timeout. If an unrecoverable error occurs, the object is converted to the "error" state. You can check the object to obtain information about the error. The object will be closed eventually. When it is in the "closed" status, the object has essentially reached the end of the state machine. Once an object is switched from a State to the next state, it will not return to the previous state. The entire process is shown in Figure 3:
Figure 3
Channel shape
Each channel implements one or more interfaces, called channel shape interfaces or channel shapes. The bottom layer of the channel shape is the IChannel interface, which provides a GetProperty <T> method to access any function exposed by the channel in the channel stack. The five channels of the extended IChannel are:
1. IInputChannel: used to receive messages
2. IOutputChannel: used to send messages
3. IRequestChannel: used to send requests
4. IReplyChannel: used to send a reply
5. IDuplexChannel: used for Bidirectional message transmission
The relationship between them is as follows:
Figure 4
In fact, we can see that the IDuplexChannel interface is the combination of the IInputChannel and IOutputChannel interfaces. All Channel shapes expand both ICommunicationObject and IChannel. These five channel models correspond to different message exchange modes. When using the datagram mode, the message sender channel implements the IOutputChannel interface, while the Message Receiver channel implements the IInputChannel interface; in the request response mode, the client channel implements the IRequestChannel interface, while the service channel implements the IReplyChannel interface. In the duplex communication mode, both parties can send and receive messages, the client and service channel implement the IDuplexChannel interface, as shown in Figure 5:
Figure 5
Server Channel
In the channel object model, in addition to the IChannel interface, the IChannelListener interface is used for the message receiver to generate a channel listener through binding (there is information about the protocol, encoding, and transmission in the binding, for details about binding, refer to the WCF topic series (6): How to transfer a message and bind Part 1) to listen for incoming messages. The channel listener is responsible for creating a channel and receiving messages from the following layer or from the network. The received messages are transmitted to the above layer through the channel created by the channel listener. The entire process is shown in Step 6:
Figure 6
In WCF, each processing of messages (that is, the channel in the channel stack) corresponds to an internal channel listener, such as the TransactionChannelListener for transaction processing and the TcpChannelListener transmitted using TCP, create corresponding channels respectively.
Client Channel
On the WCF client, the channel creation uses the channel factory. These channels are used to obtain messages from the previous layer, process the messages as necessary, and then send the messages to the next layer, 7:
Figure 7
In WCF, each message processing operation has a corresponding channel factory. For example, the TcpChannelFactory transmitted over TCP is used to create the corresponding channel.
Summary
This article describes in detail the channel programming model in WCF and the channel, channel listener, and channel factory. Most of them are purely theoretical knowledge. In the next article, we will use an example to deepen our understanding of the knowledge mentioned in this article.