Intercepting the Message body

Hi

I'm writing a custom channel + binding that can be inserted wherever in the channel stack. Its mainly used for checking some properties of the message body (in xml format).

So far I'm working off the "Custom message interceptor sample", but there seems to be no way to read the body without changing the state of the message/invalidating it.

Should I use another approach than a custom channel/binding approach What I like about it is the way you can fit it into a custom binding.

So far i've tried/read about these options:

1) In the Request(), Send() etc methods of the custom channel, create 2 copies of the message with the MessageBuffer class, read the body of the first message and send the 2nd message on to the inner channel.

2) By using a streamed transfer mode, i should be able to read a stream

3) Subclassing Message and add the message body to some buffer in the wrapper.

I think none of these methods seem quite right. I don't want to use the streamed transfer mode, and I can't rely on being able to wrap the message on the client side stack (where I also want to be able to intercept both sent & received messages).

Thanks!



Answer this question

Intercepting the Message body

  • David Notario

    Will the MessageIntercepter not work for you Does it have to be a custom binding

    If so, then you could do something like this:

    public Message Request(Message message, TimeSpan timeout)

    {

    Message ret = null;

    MessageBuffer buf = message.CreateBufferedCopy(int.MaxValue);

    Message tempmsg = buf.CreateMessage();

    XmlDictionaryReader dictread = tempmsg.GetReaderAtBodyContents();

    .......

    //recreate the original message

    message = buf.CreateMessage();

    Thanks!

    Scott



  • Alain B-H

    When you send a message, the messageIntercepter will have the message before the security elements have been applied, so nothing has been signed or encryted at that point. On the receiving side you will see that the body and custom headers have been decrypted but the security elements are there.

    So if your goal is to change the body on the client side, before it gets signed or encrypted, then your best bet would be to implment the IClientMessageInspector and make your changes there. It's much easier than creating a custom binding.

    Thanks!

    Scott



  • Ian George

    See the following posting regarding modifying the message body using the interceptor.

    http://blogs.msdn.com/vipulmodi/archive/2005/09/16/469070.aspx

    - Vipul Modi



  • Floyd Gladden

    Thanks for your reply, Scott.

    When you ask "Will the MessageIntercepter not work for you ", are you referring to the sample or some other method of intercepting messages The sample works fine.

    Your message recreation suggestion does work for me in a simple http stack, I was just worrying that this approach would not work in a WS-Security context (timestamps, message signatures etc).

    > Does it have to be a custom binding

    Not necessarily, as long as I can configure exactly at what point in the stack it should be inserted, both by configuration and programmatically.

    Thanks,

    Gert


  • Intercepting the Message body