How To Read The Base64 Encoded Xml Of A WCF Message



TODO:

Have you ever wanted to get the Base64 encoded Xml of your WCF message?

 

SOLUTION:

public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
      try
      {
           //create a copy for use
           MessageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue);
           Message newMessage = buffer.CreateMessage();
          
           //assign original so it can be used again
           request = buffer.CreateMessage();
           string messageBody = string.Empty;

           //read the message
           using (XmlDictionaryReader bodyReader = newMessage.GetReaderAtBodyContents() 
           {
                bodyReader.ReadStartElement("Binary");
                byte[] bodyBytes = bodyReader.ReadContentAsBase64();
                messageBody = Encoding.UTF8.GetString(bodyBytes);
           }

           //....do your other stuff here, the messageBody will now contain your text.
      }
      catch (Exception x)
      {
            return null;
      }
}

 

NOTES:

There are no notes on this topic.



Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading