Post-transfer series of WCF (7): binding Part 2 to transfer messages

Source: Internet
Author: User
Tags asymmetric encryption
Overview

Each service endpoint contains an Address, a Binding, and a Contract. The contract specifies available operations, and the binding specifies how to communicate with the service, and the address specifies the location of the service to be searched. In the first five articles of the WCF topic series, I learned more about the addressing details in WCF. This article describes how to bind a message in the second part. It describes in detail the various built-in binding elements of WCF, the order between binding elements, and how to create a custom binding element.

In the WCF topic series (6): binding Part 1 for message transmission, I mentioned that binding consists of binding elements, each binding element is used to describe an aspect of the end point's communication with the client. When a message is sent or received, each binding element represents a processing step, binding elements create necessary channels and listeners to generate outgoing and incoming channel stacks. Three of the main binding elements are Protocol binding elements, encoding binding elements, and transmission binding elements, let's get to know them again.

Protocol binding element

These elements represent more advanced processing steps for messages, such as reliability, security, and transactions. channels and listeners created by these binding elements can add, remove, or modify message content, to comply with the WS-* series specifications, a given binding can have zero or more Protocol binding elements, and several built-in protocol binding elements in WCF, including:

1. ReliableSessionBindingElement: This binding element provides an optional layer in the stack, which can establish a reliable session between endpoints and configure the behavior of this session. Reliability means that SOAP messages can be reliably transmitted over unreliable networks (such as HTTP) to ensure that messages are exchanged without duplicates and in the correct order, the principle is: set the same ID for a group of messages, group the messages according to the message number, and sort the messages according to the sequence number. You can refer to the WS-Reliability and WS-ReliableMessaging protocols. Its definition is as follows:

Public sealed class ReliableSessionBindingElement: BindingElement, IPolicyExportExtension {public bool Ordered {get; set;} public ReliableMessagingVersion {get; set;} // more Members}

Several built-in bindings in WCF already contain the ReliableSessionBindingElement. To add this element to other bindings, you can use the following Configuration:

<bindings>  <customBinding>    <binding name="reliabilityhttp">      <reliableSession/>    </binding>  </customBinding></bindings>

2. SecurityBindingElement: The binding element is the base class of the SOAP message Security binding element in WCF. It has three specific implementations: supports symmetric encryption channel-safe SymmetricSecurityBindingElement, asypolicricsecuritybindingelement that supports asymmetric encryption channel security, and TransportSecurityBindingElement that supports mixed-mode channel security, as shown in:

These implementations are the binding models defined in the WS-Security Policy Specification. In creating custom bindings, we need to provide a specific implementation of SecurityBindingElement, or you can customize a class to inherit from SecurityBindingElement.

3.TransactionFlowBindingElement: This binding element allows you to enable or disable the incoming transaction stream in the end point binding settings, and allows you to specify the Protocol format required for the incoming transaction. Its definition is as follows:

Public sealed class TransactionFlowBindingElement: BindingElement, IPolicyExportExtension {public TransactionProtocol {get; set;} // more member}

In the supplemented Web service specifications, there are a series of transaction processing specifications, such as WS-AtomicTransactions and WS-Coordination.

Encoding binding element

The so-called encoding is the process of converting a message into a byte sequence, while decoding is the opposite process. Encoding binding elements indicate the conversion between messages and the encoding prepared for network transmission. A typical WCF binding includes exactly one encoding binding element. The built-in encoding binding elements of WCF include MtomMessageEncodingBindingElement, BinaryMessageEncodingBindingElement, and TextMessageEncodingBindingElement. If no encoding is specified for the binding, the default encoding is used. When the transmission protocol is HTTP, the default encoding is text. For other transmission protocols, the default encoding is binary. As shown in:

1.TextMessageEncodingBindingElement: Indicates the XML message encoder encoded in text mode. It has the strongest interoperability and the lowest efficiency. Generally, the WCF Service or client can understand the text XML. However, using a large binary data block as a text transmission method is not an effective transmission method. Its definition is as follows:

Public sealed class Identifier: MessageEncodingBindingElement, IWsdlExportExtension, attributes {public int MaxReadPoolSize {get; set;} public int MaxWritePoolSize {get; set ;} public override MessageVersion {get; set ;} public Encoding WriteEncoding {get; set;} public override MessageEncoderFactory CreateMessageEncoderFactory (); // more Members}

2. BinaryMessageEncodingBindingElement:It specifies the binding element in binary XML format when the message is encoded. It contains the option used to specify the character encoding to be used and the options of the SOAP message and the WS-Addressing version. The advantage of binary encoding is the highest efficiency, and the disadvantage is the lowest interoperability. Its definition is as follows:

Public sealed class Identifier: MessageEncodingBindingElement, IWsdlExportExtension, attributes {public int MaxReadPoolSize {get; set;} public int MaxSessionSize {get; set;} public int MaxWritePoolSize {get; set ;} public override MessageVersion {get; set;} public override MessageEncoderFactory CreateMessageEncoderFactory (); // more Members}

3. MtomMessageEncodingBindingElement:Specifies the character encoding and message version management for messages encoded using the message transmission optimization mechanism (MTOM), as well as binding elements for other settings. (MTOM) is an effective technique for transmitting binary data in a WCF message. The MTOM encoder tries to establish a balance between efficiency and interoperability. MTOM encoding transmits most XML data in text format, but it optimizes the transmission of large binary data blocks by transmitting as is without converting them into base64 encoding format. Its definition is as follows:

Public sealed class Identifier: MessageEncodingBindingElement, IWsdlExportExtension, attributes {public int MaxBufferSize {get; set;} public int MaxReadPoolSize {get; set;} public int MaxWritePoolSize {get; set ;} public override MessageVersion {get; set;} public Encoding WriteEncoding {get; set;} public override MessageEncoderFactory CreateMessageEncoderFactory (); // more Members}

If the above encoding cannot meet the actual development requirements, we can also define our own message encoder. I will introduce it in detail in later articles.

Transfer binding element

These elements represent the transmission of encoded messages on the transmission protocol. A typical WCF binding includes a transfer binding element inherited from TransportBindingElement. There are several built-in transfer binding elements in WCF:

TcpTransportBindingElement HttpTransportBindingElement HttpsTransportBindingElement NamedPipeTransportBindingElement PeerTransportBindingElement MsmqTransportBindingElement MsmqIntegrationBindingElement ConnectionOrientedTransportBindingElement

In addition, you can also customize transmission binding elements.

Bind element order

When creating a binding, it is important to add the binding elements to the binding in the following order:

1. The top layer is a TransactionFlowBindingElement that allows stream transactions. Optional;

2. The following is a reliable supported ReliableSessionBindingElement. Optional;

3. The following is a SecurityBindingElement element supported by security. Optional;

4. in the next step, the message encoding binding element can be one of the three built-in message encoders in the system or a custom message encoder, which must be available. However, if you do not add one, the system will add a message encoder by default based on different transmission;

5. The bottom layer is a transmission binding element. It can be one of several built-in transmission binding elements or custom transmission binding elements.

6. For custom binding elements, you can place the binding elements between any layer above.

The following sample code creates a custom binding and adds three binding elements to it:

// Reliability supports ReliableSessionBindingElement reliable = new ReliableSessionBindingElement (); reliable. ordered = false; // The encoding element TextMessageEncodingBindingElement text = new TextMessageEncodingBindingElement (); text. messageVersion = MessageVersion. soap11WSAddressingAugust2004; // transmission element HttpTransportBindingElement http = new HttpTransportBindingElement (); http. transferMode = TransferMode. streamed; http. useDefaultWebProxy = true; // custom binding CustomBinding httpBinding = new CustomBinding (); httpBinding. name = "httpBinding"; httpBinding. elements. add (reliable); httpBinding. elements. add (text); httpBinding. elements. add (http); host. addServiceEndpoint (typeof (ICalculator), httpBinding, "http: // localhost: 8887/Calculator ");

Custom binding element

When constructing a new binding, if the built-in binding elements of the system cannot meet the requirements, you can develop a custom binding element, such as custom message binding elements, custom transmission binding elements, or any binding element. I will introduce the customization of message binding elements in the following message encoder article. The custom transmission binding element is also quite responsible for the work, you need to create a custom channel component, so it will be detailed after the introduction of the WCF channel model. In general, to implement a custom binding element, you must inherit from the abstract base class BindingElement, as shown in the following code snippet:

public class LoggingBindingElement : BindingElement{    public LoggingBindingElement()    {    }    public LoggingBindingElement(LoggingBindingElement other)        : base(other)    {    }    public override BindingElement Clone()    {        return new LoggingBindingElement(this);    }    public override T GetProperty<T>(BindingContext context)    {        if (context == null)        {            throw new ArgumentNullException("context");        }        return context.GetInnerProperty<T>();    }}

In the following article, we will see an example of a complete custom binding element.

Summary

This article describes how to bind a message in the second part. It describes in detail the various built-in binding elements of WCF, the order between binding elements, and how to create a custom binding element.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.