From the hierarchy of the entire infrastructure, WCF can be divided into two tiers of the service model layer and the channel layer. The service model layer is built on the channel layer and provides a unified. Extensible programming model, while the channel layer realizes the transmission and processing of messages through the channel stack.
3.1 Channel and channel stacks
The channel layer consists of a channel stack created by the binding, and WCF builds the service model layer on top of the channel layer to provide an object-oriented application programming interface.
Channel stacks created by the service model Layer channel layer of WCF
3.1.1 Channel and channel stack
For the channel stack of WCF, there are also two required channels, the transmit channel (Transport channel) and the Message encoding channel (the Messages Encoding channels).
WCF uses message-based communication, and all features, whether business-related or business-agnostic, are implemented through message exchange. In addition to the basic message encoding and transmission of the mouth, some other functions also need to be implemented by adding some corresponding message processing operations in the message Exchange project, typical features include:
Transaction flow (Transaction flowing): Transfers the transaction from the client to the server, thereby incorporating the execution of the service into the transaction.
Secure Transport (Transfer Security): Secure packets or messages, avoid malicious tampering and snooping, and resolve client and service identity authentication issues
Reliable transmission (Reliable Messaging): Ensure the reliable and orderly transmission of packets or messages in a stable network environment.
Support for most of the WS-* protocols is achieved by adding the appropriate channel to the channel stack. 3-2, above the transmission channel and the message encoding channel, the ws-security-based channel ensures the transmission security of the message, and the channel based on WS-RM (Ws-reliable message) realizes the reliable transmission of the message; Based on Ws-at (ws-atomic Transaction) channels enable distributed things to support.
3.1.2 Example Demo: Message communication directly through bindings
The receiver code is as follows:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.ServiceModel;usingSystem.ServiceModel.Channels;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Xml.Linq;namespacereceiver{classProgram {Static voidMain (string[] args) {Uri Listeninguri=NewUri ("Http://127.0.0.1:3721/listener"); Binding binding=NewBasicHttpBinding (); //Create, turn on the listenerIchannellistener<ireplychannel> Channellistener = binding. Buildchannellistener<ireplychannel>(Listeninguri); Channellistener.open (); //Create, turn on the reply channelIReplyChannel channel =Channellistener.acceptchannel (TimeSpan.MaxValue); Channel. Open (); //Start listening . while(true) { //receive input MessagesRequestContext RequestContext =Channel. Receiverequest (TimeSpan.MaxValue); Console.WriteLine (Requestcontext.requestmessage); //message replyrequestcontext.reply (Createrelymessage (binding)); } } StaticMessage Createrelymessage (binding binding) {stringAction ="Http://www.artech.com/calculatorservice/AddResponse"; XNamespace NS="http://www.artech.com"; XElement Body=NewXElement (NewXElement (ns +"Addresult",3)); returnMessage.createmessage (binding. MessageVersion, action, body); } }}
View Code
The sender code is as follows:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.ServiceModel;usingSystem.ServiceModel.Channels;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Xml.Linq;namespacesender{classProgram {Static voidMain (string[] args) {Uri Listeninguri=NewUri ("Http://127.0.0.1:3721/listener"); Binding binding=NewBasicHttpBinding (); //Create, turn on the listenerIchannelfactory<irequestchannel> ChannelFactory = binding. Buildchannelfactory<irequestchannel>(Listeninguri); Channelfactory.open (); //Create, turn on the reply channelIRequestChannel channel = Channelfactory.createchannel (Newendpointaddress (Listeninguri)); Channel. Open (); Message Relymessage=Channel. Request (CreateRequestMessage (binding)); Console.WriteLine (Relymessage); Console.read (); } StaticMessage CreateRequestMessage (binding binding) {stringAction ="HTTP://WWW.ARTECH.COM/CALCULATORSERVICE/ADD"; XNamespace NS="http://www.artech.com"; XElement Body=NewXElement (NewXElement (ns +"ADD",3,NewXElement (ns +"x",1),NewXElement (ns +"y",2))); returnMessage.createmessage (binding. MessageVersion, action, body); } }}
View Code
Receiver
Sender
3.1.3 The binding model of WCF
In the entire binding model, the channel and channel stacks are at the lowest level. The channel stack is the channel of message communication, and each channel composing the channel stack focuses on the realization of a specific message processing function. Depending on the functionality provided, the channel can be divided into 3 classes.
- Transmission channel (Transport Channel): Enables the transmission of messages based on some kind of network protocol (HTTP, HTTPS,TCP,MSMQ, Named pipes, etc.).
- Message Encoding channel (MSG Encoding Channel): The encoding of messages is implemented, and the common message encoding methods are text/xml,binary and Mtom.
- Protocol channel (Protocol Channel): Enables WCF to support several WS-* protocols, such as Ws-security, WS-RM, Ws-at
In WCF, channels are created through the Channel Manager (ChannelManager). In fact, channel managers are collectively known as channel listeners and channel factories.
The channel manager has a different name on the server side and the client because it does not work the same on the client and the server. The role of the channel manager on the server is to create a channel stack listener and accept request messages, so called channel listeners, and the client's channel manager is called a channel factory because it creates a channel for sending and replying messages to the request message.
3.2 Channel and channel stacks
3.2.1 Communicationobject
The WCF binding model involves many types of components, such as channels, channel listeners, channel factories, and so on. From the functionality, these objects are for communication services and can be collectively referred to as communication objects.
3.2.2 Defaultcommunicationtimeouts
Default Time-out
3.2.3 IChannel and Channelbase
3.2.3 Message Exchange Mode
Datagram Mode (Datagram)
Send/Forget (Send/forget) or unidirectional (one-way) mode
- Single destination mode: The sender of a message sends a message to a single receiver.
- Multi-cast/broadcast mode: The sender of a message sends a message to multiple recipients.
The datagram mode generally uses asynchronous message sending method, does not want to receive the reply message of the other party, in some cases even do not care about the message can be accepted normally.
Request-Reply mode (request-reply)
The request-reply mode generally uses synchronous communication mode (although the change mode can also be used for asynchronous communication).
Duplex Mode (Duplex)
In the case of a duplex message exchange pattern, either party can send a message to the other during a message exchange. Duplex communication makes it possible for the service side to callback client operations.
The more typical duplex mode is the subscription/publish mode, in which the roles of the two sides of the message exchange change from the traditional sender/receiver to the Subscriber and publisher. The subscription direction the publisher sends a subscription request to a topic, and the Publisher accepts the subscription message and adds the Subscriber to the list of subscriptions. When the topic is published, the publisher extracts all Subscribers to the current topic and broadcasts the message to them.
The exchange of messages relies on network transport, and different network transport protocols have different support methods for duplex communication. For TCP, because the protocol itself is a network communication protocol that supports full duplex, it is able to provide native support for duplex communication. But for the HTTP protocol itself, it is a simple request-reply network protocol that does not support duplex communication. Wsdefaluthttpbinding-based duplex communication is actually implemented with two associated HTTP channels.
3.2.5 Channel shape
3.2.6 Session Channel
From the state-holding angle, the channel can be divided into the datagram Channel (Datagram channels) and the back channel (Sessionful channel). The former does not need to maintain the state of a specific client (service proxy), so multiple client objects can use the same channel, while the latter binds a client object and has the same life cycle as the client object.
A session in WCF represents a context that is shared between the participants who are communicating, and the session is implemented by means of a message Correlation. The so-called Message Association, is to send from the same client message through a session ID associated together.
3.2.7 Example Demo: Custom Channel
3rd Chapter Binding