Search code examples
wcfwcf-clientmtom

WCF - Inspect an MTOM encoded message BeforeSendRequest


I'm integrating with a third party WCF service that is using Mtom as the message encoding. I have created a message inspector behavior, and i'm able to view the message request "string" by calling request.ToString(), However, the message never appears to be mtom encoded, and doesn't include any MIME parts. I'm assuming the Mtom encoding happens later in the channel pipeline. My question is, is there a way to view the actual outgoing message regardless of encoding as it will be sent over the wire to the WCF service?

Below is the message inspector i'm using:

public class InspectorBehaviorExtensionElement : BehaviorExtensionElement
{
    public InspectorBehaviorExtensionElement()
    {

    }

    public override Type BehaviorType
    {
        get
        {
            return typeof(InspectorBehavior);
        }
    }

    protected override object CreateBehavior()
    {
        return new InspectorBehavior();
    }

}

public class InspectorBehavior : IEndpointBehavior
{
    public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
        clientRuntime.MessageInspectors.Add(new MessageInspector());
    }

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {
    }

    public void Validate(ServiceEndpoint endpoint)
    {
    }
}

public class MessageInspector : IClientMessageInspector
{
    public MessageInspector()
    {

    }
    public void AfterReceiveReply(ref Message reply, object correlationState)
    {
        Debug.WriteLine("Received the following reply: '{0}'", reply);
    }

    public object BeforeSendRequest(ref Message request, IClientChannel channel)
    {
        Debug.WriteLine("Sending the following request: '{0}'", request);

        return null;
    }
}

Solution

  • AFAIK message encoding is applied after BeforeSendRequest. You may use WCF Message logging or fiddler to see the messages.