October 30, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

The WCF MessageEncoder Decoded

  • June 1, 2007
  • By Jeffrey Juday
  • Send Email »
  • More Articles »

Bytes to Messages

The following code loads a stream, copies the stream to a buffer using a buffer manager, and calls ReadMessage on the TestMessageEncoder to create a Message class.

buffer = mgr.TakeBuffer((int)streamFrom.Length);

streamFrom.Read(buffer, 0, (int)streamFrom.Length);

byteArray = new ArraySegment<byte>(buffer);

msg = _encoder.ReadMessage(byteArray, mgr);

The invoked ReadMessage follows.

public override Message ReadMessage(Stream stream,
   int maxSizeOfHeaders, string contentType)
{
   Message msg;
   XmlDocument doc = new XmlDocument();

   doc.Load(stream);

   TestBodyWriter bw = new TestBodyWriter(doc);

   msg = Message.CreateMessage(this.MessageVersion,"",bw);

   return (msg);
}

Your first question is probably what is this ArraySegment<byte> generic class? According to the .NET Framework documentation, an ArraySegment works in conjunction with the Array class, providing a "wrapper" around a portion of the underlying Array.

As you may have noticed, CreateMessage on the Message class provides many ways to build a message class. Reviewing all the overloaded CreateMessage variations is beyond the scope of this article.

Instead, I chose one of the BodyWriter class variations offering the most control over the contents of the "body" of the message. My BodyWriter implementation serves as a wrapper for the XmlDocument class. Your own implementation of the BodyWriter will likely use some of the .NET classes I described earlier in the article.

Next, you'll see how to change the Message class you created back to bytes.

Messages Back to Bytes

Variations of the WriteMessage function copy a Message class to bytes and streams. My implementation of the WriteMessage function appears below.

public override void WriteMessage(Message message, Stream stream)
{

   XmlWriter writer = XmlWriter.Create(stream);
   XmlDictionaryWriter xmlDW =
      XmlDictionaryWriter.CreateDictionaryWriter(writer);

   message.WriteBodyContents(xmlDW);
   xmlDW.Close();

}

The implementation is typical. I used the XmlDictionalWriter class to write the message body data to a stream. As the Message is written to the stream because you created the message with a BodyWriter class; the OnWriteBodyContents in the BodyWriter is invoked. As stated earlier, the BodyWriter is a wrapper for the XmlDocument class and therefore simply dumps contents from the XmlDocument to the XmlWriter parameter.

Conclusion

MessageEncoders convert the bytes coming over the wire to a WCF Message class. A MessageEncoder normally resides inside of a Transport Channel. Like many other WCF classes, MessageEncoders have an accompanying binding and factory class. Message encoders typically utilize XmlReaders, XmlWriters, and stream classes in the .NET Framework.

Download the Code

You can download the code that accompanies this article here.

Sources

About the Author

Jeffrey Juday is a software developer with Crowe Chizek in South Bend, Indiana. He has been developing software with Microsoft tools for more than 12 years in a variety of industries. Jeff currently builds solutions using BizTalk 2004, ASP.NET, SharePoint, and SQL Server 2000. You can reach Jeff at jjuday@crowechizek.com.



Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel