[ACCEPTED]-Get Just the Body of a WCf Message-soap

Accepted answer
Score: 22

You can access the body of the message by 2 using the GetReaderAtBodyContents method 1 on the Message:

using (XmlDictionaryReader reader = message.GetReaderAtBodyContents())
{
     string content = reader.ReadOuterXml();
     //Other stuff here...                
}
Score: 5

Not to preempt Yann's answer, but for what 6 it's worth, here's a full example of copying 5 a message body into a new message with a 4 different action header. You could add or 3 customize other headers as a part of the 2 example as well. I spent too much time writing 1 this up to just throw it away. =)

class Program
{
    [DataContract]
    public class Person
    {
        [DataMember]
        public string FirstName { get; set; }

        [DataMember]
        public string LastName { get; set; }

        public override string ToString()
        {
            return string.Format("{0}, {1}", LastName, FirstName);
        }
    }

    static void Main(string[] args)
    {
        var person = new Person { FirstName = "Joe", LastName = "Schmo" };
        var message = System.ServiceModel.Channels.Message.CreateMessage(MessageVersion.Default, "action", person);

        var reader = message.GetReaderAtBodyContents();
        var newMessage = System.ServiceModel.Channels.Message.CreateMessage(MessageVersion.Default, "newAction", reader);

        Console.WriteLine(message);
        Console.WriteLine();
        Console.WriteLine(newMessage);
        Console.WriteLine();
        Console.WriteLine(newMessage.GetBody<Person>());
        Console.ReadLine();
    }
}

More Related questions