Introduction
In essence, WCF is a Communications service framework that allows us to use different transport protocols to interact with different WS-* Series specifications using different message encoding forms, all of which are handled by the channel stack. To simplify these processes, two models are available in WCF, one for the developer's application programming model, and the other for the communication channel model, so that for developers, as long as they understand the application programming model is sufficient, without involving the channel model, however, for the channel model to do the necessary learning, It allows us to really understand the concept of "communication" in WCF, to understand the entire architecture of WCF, to build more robust WCF services, or to extend the WCF framework. In this article, we'll take a deep look at 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 a lower-level programming model for message delivery and reception, which is called the WCF channel model. A very important concept in the channel model is the channel stack, which is a layered communication stack with one or more message processing channels, with various types of channels placed in the stack, used for object processing, such as placing a transport channel at the very bottom of the channel stack, which is responsible for adapting the channel stack to the underlying transport, as shown in 1:
Figure 1
In the channel stack, not only the transmission of the message is provided, but also provides other functions such as the content of the message or the message header processing, these functions are also placed in the channel stack as a channel, even we can write their own channel, added to the channel stack.
When a message travels through the channel stack, it flows through the communication stack as a message object, such as the transport channel is responsible for translating the message between the sender and the receiver, and then the message continues upstream through the transport channel, sequentially passing through each channel in the channel stack, each of which is responsible for providing a communication function. such as adding information to the message header, encrypting the body of the message, and so on.
Channel Object Model
The channel object model is a core set of interfaces required to implement a channel, channel listener, and channel factory. Some base classes are also available to assist with custom implementations. You can see that the most important of the channel models are three sets of interfaces: Channel, channel Listener, and channel factory. Each channel implements one or more interfaces, called Channel shape interfaces or channel shapes, and the channel listener is responsible for listening for incoming messages, that is, at the receiving end of the message, and then passing the messages to the upper layer through a channel created by the channel listener, which is responsible for creating the channel for sending messages, that is, in the sender of the message, And when the channel factory shuts down, all channels created by the channel factory are closed.
In the channel model, one of the most important interfaces is Icommunicationobject, which defines the core interface of the basic state machine implemented by all communication objects, and the custom channel communication object can be directly implemented as shown in icommunicationobject,2:
Figure 2
Only those interfaces in Figure 2, which are part of the WCF channel model, also provide some base classes such as Communicationobject, channelfactorybase, etc. in WCF, and can be directly inherited from these base classes when implementing custom channel communication objects, but be aware that , these base classes simply provide the convenience of implementing custom channel communication objects, which are not themselves part of the channel model.
The Icommunicationobject interface provides a contract for all communication-oriented objects in WCF, in addition to channels, channel listeners, and channel factories, as well as schedulers and service hosts, which define the basic state of the contract. It includes a set of open, close, and abort methods for initiating a state transition, an asynchronous version of the open and Close method, a set of events that provide state transition notifications, and an exposed state property that checks the status of an object, as defined below:
public interfaceicommunicationobject{State propertiesCommunicationstate State {Get }EventEventEventHandler Closed;EventEventHandler Closing;EventEventHandler Faulted;EventEventHandler opened;EventEventHandler Opening;Methodvoid Abort ();void Close ();void Close (TimeSpan timeout);void Open ();void Open (TimeSpan timeout);Async methodsIAsyncResult Beginclose (asynccallback callback, object state); iasyncresult beginclose (timespan timeout, 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 state of Icommunicationobject is created, at which point you can configure its various properties. Once in the open state, the object can be used to send and receive messages, but its properties are considered immutable. Once in the "shutting down" state, the object can no longer process new send or receive requests, but it is possible to complete an existing request before reaching a "close" timeout. If an unrecoverable error occurs, the object is converted to an "error" state, at which point the object can be inspected for information about the error, and the object will eventually close. When in the closed state, the object essentially reaches the end point of the state machine. Once an object transitions from one state to the next, it will not return to the previous state, as shown in Figure 3:
Figure 3
Channel shape
Each channel implements one or more interfaces, called Channel shape interfaces or channel shapes. The bottom of the channel shape is the IChannel interface, which provides a getproperty<t> method that serves as a layered mechanism for accessing any functionality exposed by the channels in the channel stack. The five channel shapes for the extended IChannel are:
1.IInputChannel: for receiving messages
2.IOutputChannel: for sending messages
3.IRequestChannel: for sending requests
4.IReplyChannel: for sending replies
5.IDuplexChannel: For two-way message delivery
The relationship between them is shown in 4:
Figure 4
As you can see, the Iduplexchannel interface is a union of Iinputchannel and Ioutputchannel interfaces, and all channel shapes expand Icommunicationobject and IChannel at the same time. These 5 channel models correspond to different message exchange modes, when using datagram mode, the message sender channel implements the Ioutputchannel interface, and the message receiver channel implements the Iinputchannel interface, and in the request response mode, The client channel implements the IRequestChannel interface, and the service channel implements the IReplyChannel interface, in the duplex communication mode, the client and the service channel implement the Iduplexchannel interface because both can send and receive messages, 5:
Figure 5
Service-Side Channel
In the channel object model, in addition to the IChannel interface, another interface Ichannellistener for the receiving end of the message, by binding to generate a channel listener (in the binding there is information about the protocol, encoding and transmission, About bindings you can refer to the WCF Feature series (6): How messages are passed by binding Part 1), which is used to listen for incoming messages. The channel listener is responsible for creating the channel and receiving messages from the following layer or from the network, and the received message is routed to the upper layer using the channel created by the channel listener. The entire procedure is shown in 6:
Figure 6
Within WCF, for each processing of a message (that is, a channel in the channel stack), an internal channel listener is corresponding, such as Transactionchannellistener for transactions and Tcpchannellistener with TCP transmissions. Used to create each corresponding channel.
Client Channel
In WCF clients, channel creation uses the channel factory, which is responsible for obtaining messages from the previous layer, processing the message as necessary, and then sending the message to the next layer, as shown in 7:
Figure 7
Within WCF, there is a corresponding channel factory for every processing of messages, such as tcpchannelfactory with TCP transmission, to create the corresponding channel.
Summarize
This article details the Channel programming model in WCF, as well as about channels, channel listeners, and channel factories, most of which are purely theoretical knowledge, and in the next article, an example will be taken to deepen the knowledge of this article.
"Collection to" WCF Post series (8): Depth channel programming model Part 1-Design