Inspecting WCF messages

Quite often during the WCF implementation for services layer, we find a need to interrogate the messages inbound as well as outbound at Server (Services layer) and sometimes the same applies for the Client applications too.

At a small (and I mean extremely small) data transaction shop, we realized a need to inspect SOAP XML messages inbound to our Services layer and the first thought was to leverage Fiddler. The tool Fiddler does do a good job but for some reason we could never get its reverese proxy work with our Secured Services layer (using https). Fiddler does wonderful job when running on the client machine to trace Request and Response SOAP XMLs but then by integrating SOAPExtensionAttribute I'd rather do the same from within my own application. The need for us was to trap messages before it hits the Services layer!

Anyway, so the need for inspecting messages at a WCF Services layer led me to explore the WCF extensibility model and there we have it.. WCF has a concept of Message Inspectors tied to two major interfaces; IDispatchMessageInspector (for the Server side) and IClientMessageInspector (for the Client side)
Implement IDispatchMessageInspector to inspect or modify inbound or outbound messages either prior to dispatching a request message to an operation or before returning a reply message to a caller. There are a large number of scenarios that require intercepting messages prior to invoking the operation for which it is destined. For example, you can log incoming application messages or perform some feature based on a message header. 
  • The AfterReceiveRequest method enables custom behavior after receiving the message but before dispatching it to the intended operation.
  • The BeforeSendReply method enables custom behavior after the operation returns but before the reply is sent.
Implement the IClientMessageInspector interface to inspect or modify messages as they pass through the client application. At the client side, IClientMessageInspector implementations get a chance to inspect the message after it has been created based on the operation call, and right before it’s encoded. BeforeSendRequest receives the message and the channel information through which the message is being sent. When the response from the server arrives and is decoded (by the message encoder) into a Message object, it’s passed to the client inspector in it AfterReceiveReply method.

Comments

Popular posts from this blog

Baseless merge!

QuadKey - what it is?

Binding ENUM to WPF control - ComboBox