Summary of several methods for intercepting messages in WCF

Source: Internet
Author: User

WCF is based on the message mechanism. Some functions, such as addressing and encryption, are fully reflected in messages. The channels in WCF are like the pipeline in the workshop, messages are the products to be processed in this pipeline. Each time a message is processed, the corresponding channel processes different operations on the message. A clear understanding of the message processing process is of great practical significance to the working principle, debugging tracking, custom scaling, and performance security of WCF. To intercept messages, you can use the following methods:

1 routing Interception Method

2. Custom binding

3. imessageinspector interface implementation

4. Tracking and Diagnosis

The difficulty and effect of the four methods are different. The following describes the implementation methods one by one.

1 routing Interception Method

This method can be divided into two types:

A) Use existing TCP Message tracing tools.

B) Use the routing feature in WCF for custom routing.

Among them, the common ready-made TCP Message tracing tools are:

A) tcptrace

B) tool soap trace utility included in soap Toolkit 3.0

The two tools work in the same principle, as shown in:

The two tools are used to intercept messages. For example, early on, artech was on its blogArticleThis document introduces the use of tcptrace. The article is [original] My WCF journey (9): How to Use tcptrace for soap trace in WCF. So I will not go into details about the specific usage. To sum up, when tcptrace or soap trace utility is used, listen port specifies the listening address of the route and the Via address set by the client. The listening address of the service to be set for destination port. On the way above, the listen port should be 8019, And the destination port should be 8020

In addition to using existing tools for routing, WCF also supports routing. The implementation principles are consistent. The only difference is that the intermediate router can be compiled by itself using the WCF technology. In this way, it is more flexible. A simple demo is provided, which is mainly applicable to the interaction mode of request/reply.

There are five projects in the demo:

Jillzhang. WCF. messageinspectors. Contracts

Jillzhang. WCF. messageinspectors. Services

Jillzhang. WCF. messageinspectors. Host

Jillzhang. WCF. messageinspectors. Client

Jillzhang. WCF. messageinspectors. rounter

The first four components create a simple WCF Service and client to form a distributed system. The most important thing is jillzhang. WCF. messageinspectors. the rounter project can receive all messages by setting the action and replyaction of servicecontract to * and setting the addressfiltermode of servicebehavior to any, do not filter the action or body of a message. DetailsCodeIf you are interested, you can download the Demo code.

Well, the two methods of the routing interception method are described in detail, so this method has its advantages and disadvantages, they are:

Advantages:

1) the routing analysis method does not need to modify the code of the original client and server too much, and the coupling degree with the original architecture is also relatively low.

2) the routing analysis method can reflect the powerful routing functions in WCF.

Disadvantages:

The disadvantages of the routing analysis method are also significant:

1) It can only intercept sent request messages but cannot intercept corresponding responses.

2) the routing method adds nodes in the communication process, so that efficient transmission security cannot be used to ensure information security.

 

2. Custom binding

I remember talking about the trustworthy session in WCF in the previous article and talking about the binding organizational structure. In the above description, we emphasize that WCF is a message-based communication method, and the most important concept is the channel. Let's take another example and look at the following figure of the food digestion process:

We can make the message into the total food shown in the figure above. For example, the mouth, esophagus, stomach, large intestine, small intestine, and anus can be seen as a channel for food processing. They perform their respective duties, process the food in sequence. In the channel of WCF, the channel can also perform various processing on the message, such as encryption, decryption, encoding, decoding, click-and-select, delete, add, and modify. I will draw another figure to show the message processing process in the channel:

 

Messages are ordered through the channel in each bindingelement in binding, so that we can implement a custom channel specifically used to record messages. this is the theoretical basis and implementation idea for implementing custom binding to intercept messages. This idea is also realized through actual encoding:

First, let's take a look at the Channel implementation method. In WCF, the channel is organized to the namespace of system. servicemodel. channels. All channels must implement the system. servicemodel. channels. ichannel interface. In fact, the ichannel interface only defines three types of operations: open, close, and abort. These operations are based on sockets. Therefore, each operation is synchronized and asynchronously distinguished. The following commonly used channel interface definitions are also provided in the system:

1) irequestchannel

In WCF, only the channel that implements the irequestchannel can become the request end for Request/reply communication. In addition, irequestchannel defines the request. This operation is used to send the request message and view the message content in this operation. In addition, remoteaddress and via are added to indicate the remote address of the message sending and the transmission address of the message sending request respectively.

2) ireplychannel

The ireplychannel corresponds to the irequestchannel. Only the channel that implements the ireplychannel can be used as the response end of request/reply, and the receiverequest operation is added. This operation is responsible for receiving requests, you can view the message content in this operation.

3) iduplexsessionchannel

This channel defines the association between duplex channels and sessions, and provides send and receive operations. Both operations can view the message content. In addition, iduplexchannel and isessionchannel can be associated through session.

There are many other common channels, such as iinputchannel, ioutputchannel, and iduplexchannel. For details, refer to msdn. But the above three are very important. The above two irequestchannels and ireplychannels can be used to use the binding of httptransportbindingelement, while the iduplexsessionchannel can be used to use the binding of tcptransportbindingelement.

Through the channel, you can view the log message content at the specified position of the message, and then implement a custom binding to intercept the message.

The demo contains the following files:

Logrequestchannel. CS: implements the irequestchannel for the request end.

Logreplychannel. CS implements ireplychannel for the responder

Logduplexsessionchannel. CS implements iduplexsessionchannel

Logchannellisterner. CS implements ichanellistener, which is the listener of the server to the channel.

Logchannelfactory. CS implements ichanelfactory and is the manager of the client channel.

Loghttpbinding. CS is used for binding custom log messages in http mode

Logtcpbinding. CS is used for binding custom log messages in TCP mode.

Check the final result. When loghttpbinding is used:

Server:

Client:

When logtcpbinding is used:

Server:

Client:

From the figure above, we can see that the extended binding can also intercept the message content.

So what are the advantages and disadvantages of this method?

Advantages:

1) can intercept messages in two aspects: request and response.

2) messages can be obtained through the channel.

3) The captured position is flexible.

The disadvantage is that the implementation is complicated.

 

There are four methods. Let's talk about these two methods first. The following two methods are reserved for the next article. The article is long and really tired.

Example project: http://files.cnblogs.com/jillzhang/Jillzhang.Wcf.MessageInspector.rar

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.