Embedd_0913
Embedd_0913

Reputation: 16565

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.

Upvotes: 4

Views: 23807

Answers (2)

Jesse Chisholm
Jesse Chisholm

Reputation: 4026

I found a foible in Microsoft's implementation of Message.ToString(). Then I figured out the cause and found a solution.

Message.ToString() may have the Body contents as "... stream ...".

This means that the Message was created using an XmlRead or XmlDictionaryReader that was created from a Stream that hasn't been read yet.

ToString is documented as NOT changing the State of the Message. So, they don't read the Stream, just put in a marker that there is on.

Since my goal was to (1) get the string, (2) alter the string, and (3) create a new Message from the altered string, I needed to do a little extra.

Here's what I came up with:

/// <summary>
/// Get the XML of a Message even if it contains an unread Stream as its Body.
/// <para>message.ToString() would contain "... stream ..." as
///       the Body contents.</para>
/// </summary>
/// <param name="m">A reference to the <c>Message</c>. </param>
/// <returns>A String of the XML after the Message has been fully
///          read and parsed.</returns>
/// <remarks>The Message <paramref cref="m"/> is re-created
///          in its original state.</remarks>
String MessageString(ref Message m)
{
    // copy the message into a working buffer.
    MessageBuffer mb = m.CreateBufferedCopy(int.MaxValue);

    // re-create the original message, because "copy" changes its state.
    m = mb.CreateMessage();

    Stream s = new MemoryStream();
    XmlWriter xw = XmlWriter.Create(s);
    mb.CreateMessage().WriteMessage(xw);
    xw.Flush();
    s.Position = 0;

    byte[] bXML = new byte[s.Length];
    s.Read(bXML, 0, s.Length);

    // sometimes bXML[] starts with a BOM
    if (bXML[0] != (byte)'<')
    {
        return Encoding.UTF8.GetString(bXML,3,bXML.Length-3);
    }
    else
    {
        return Encoding.UTF8.GetString(bXML,0,bXML.Length);
    }
}
/// <summary>
/// Create an XmlReader from the String containing the XML.
/// </summary>
/// <param name="xml">The XML string o fhe entire SOAP Message.</param>
/// <returns>
///     An XmlReader to a MemoryStream to the <paramref cref="xml"/> string.
/// </returns>
XmlReader XmlReaderFromString(String xml)
{
    var stream = new System.IO.MemoryStream();
    // NOTE: don't use using(var writer ...){...}
    //  because the end of the StreamWriter's using closes the Stream itself.
    //
    var writer = new System.IO.StreamWriter(stream);
    writer.Write(xml);
    writer.Flush();
    stream.Position = 0;
    return XmlReader.Create(stream);
}
/// <summary>
/// Creates a Message object from the XML of the entire SOAP message.
/// </summary>
/// <param name="xml">The XML string of the entire SOAP message.</param>
/// <param name="">The MessageVersion constant to pass in
///                to Message.CreateMessage.</param>
/// <returns>
///     A Message that is built from the SOAP <paramref cref="xml"/>.
/// </returns>
Message CreateMessageFromString(String xml, MessageVersion ver)
{
    return Message.CreateMessage(XmlReaderFromString(xml), ver);
}

-Jesse

Upvotes: 11

Brian
Brian

Reputation: 118905

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.

Upvotes: 3

Related Questions