WCF custom encoding: Message Manipulations

Damir Dobric Posts

Next talks:

 

    

Follow me on Twitter: #ddobric



 

 

Archives

During extensting of WCF, I ofter run into problem of message manipulation. This is mostly required by implemnting of custom encoding mechanism in WCF.

In that scenario you will have to do one or more of following actions:

 

-         Serialize the message in the stream

-         Serialize the message in the buffer

-         Encode the message

-         Decode the message

-         Deserialize the message from the buffer

-         Desrialize the message from the stream.

-         Add/Read the custom message header.

 

Here are some useful code-snippet which I mostly used:

 

Adding of the custom header to the existing message:

 

MessageHeader myHeader =

MessageHeader.CreateHeader("myHeader", "http://www.daenet.eu", "SomeXslFile");

msg.Headers.Add(myHeader);

  

Reading of the custom message header:       

 

string myHeader =

msg.Headers.GetHeader<string>("myHeader", "http://www.daenet.eu");

 

Searializing and encoding of the message in the memory stream:

 

XmlWriterSettings xmlSettings = new XmlWriterSettings();

xmlSettings.Encoding = Encoding.GetEncoding("iso-8859-1");

           

MemoryStream stream = new MemoryStream();

XmlWriter writer = XmlWriter.Create(stream, xmlSettings);

msg.WriteMessage(writer);

writer.Close();

 

Creating of the buffer from the serialized and encoded message stream:

 

byte[] binaryMessage = stream.GetBuffer();

int messageLength = (int)stream.Position;

stream.Close();

 

Creating of the ArraySegment

ArraySegment is mostly used type by implementing of wcf custom encoder.

 

ArraySegment<byte> segmentArray =

new ArraySegment<byte>(binaryMessage, 0, binaryMessage.Length);

 

Utilizing of the BufferManager

The BufferManger is a helper class used by wcf runtime. More about this class yau can find by Nicholas Allen’s blog (probably best source for undocumented WCF staffs).

 

BufferManager buffMgr = BufferManager.CreateBufferManager(2048, 2048);

byte[] buff = buffMgr.TakeBuffer(binaryMessage.Length);

Array.Copy(binaryMessage, buff, binaryMessage.Length);

buffMgr.ReturnBuffer(buff);

 

byte[] pooledBuff = buffMgr.TakeBuffer(binaryMessage.Length);

       

ArraySegment<byte> segmentArray =

new ArraySegment<byte>(pooledBuff, 0, binaryMessage.Length);

 

Creating of the buffer from the ArraySegment:

byte[] msgBuffer = new byte[segmentArray.Count];

 

Array.Copy(segmentArray.Array,

segmentArray.Offset, msgBuffer, 0, msgBuffer.Length);

 

Deserialize Message form binary data:

MemoryStream stream = new MemoryStream(msgBuffer);

XmlReader reader = XmlReader.Create(stream);

Message msg = Message.CreateMessage(reader, maxSizeOfHeaders);

 


Posted Jul 16 2006, 04:52 PM by Damir Dobric
Filed under:

Comments

Damir Dobric Posts wrote Cloning of the Message
on 01-25-2007 13:27

By writing of custom Message Inspectors (classes derived from IClientMessageInspector and IDispatchMessageInspector

Damir Dobric wrote re: WCF custom encoding: Message Manipulations
on 04-09-2007 12:44
developers.de is a .Net Community Blog powered by daenet GmbH.