Search code examples
wcf

How to get message content from System.ServiceModel.Channels.Message?


I have a message contract which i am passing to my wcf service and i am having a message inspector which i m using to find what was sent by the wcf client. I have the Message but i don't know how to get the data from it. following is my message request which i am passing to wcf service.

[MessageContract]
public  class MyMessageRequest
{
    [MessageBodyMember]
    public string Response
    {
        get;
        set;
    }

    [MessageHeader]
    public string ExtraValues
    {
        get;
        set;
    }
}

The method where i am getting the Message is following:

public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
{
        MessageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue);

        request = buffer.CreateMessage();
        Console.WriteLine("Received:\n{0}", buffer.CreateMessage().ToString());
        return null;
}

I want to see the values of Response and ExtraValues out of the message , Please anyone help me out in this.


Solution

  • I think you want

    http://msdn.microsoft.com/en-us/library/system.servicemodel.description.typedmessageconverter.frommessage.aspx

    where a

    (new TypedMessageConverter<MyMessageRequest>()).FromMessage(msg)
    

    will give you back the object you need.