Paulo Morgado

.NET Development & Architecture

This Blog

Syndication

Search

Sponsored By

Tags

News

Unit Test Today! Get Typemock Isolator!

Books

 

Visitors

Visitor Locations

Community

Email Notifications

Archives

Profile

Disclaimer

The opinions and viewpoints expressed in this site are mine and do not necessarily reflect those of Microsoft, my employer or any community that I belong to. Any code or opinions are offered as is. Products or services mentioned are purchased by me, made available to me by my employer or the manufacturer/vendor which doesn't influence my opinion in any way.

Optimizing writing for the CustomTextMessageEncoder

As I told before I've been using WFC to call legacy POX web services. Some of them only accept iso-8859-1, others can accept utf-8, but all use MessageVersion.None and the text/xml media type.

But since the new XmlDictionaryWriter is optimized for utf-8, utf-16 and Unicode character encodings and will perform better that the XmlTextWriter I would like to be able to use it when possible. To achieve this, two methods of the CustomTextMessageEncoder need to be changed in order to use the best writer.

public override ArraySegment<byte> WriteMessage(Message message, int maxMessageSize, BufferManager bufferManager, int messageOffset)

{

    MemoryStream stream = new MemoryStream();

    WriteMessage(message, stream);

    //XmlWriter writer = XmlWriter.Create(stream, this.m_writerSettings);

    //message.WriteMessage(writer);

    //writer.Close();

 

    byte[] messageBytes = stream.GetBuffer();

    int messageLength = (int)stream.Position;

    stream.Close();

 

    int totalLength = messageLength + messageOffset;

    byte[] totalBytes = bufferManager.TakeBuffer(totalLength);

    Array.Copy(messageBytes, 0, totalBytes, messageOffset, messageLength);

 

    ArraySegment<byte> byteArray = new ArraySegment<byte>(totalBytes, messageOffset, messageLength);

    return byteArray;

}

 

public override void WriteMessage(Message message, Stream stream)

{

    if ((this.m_writerSettings.Encoding.WebName == ValidatingUTF8.WebName) || (this.m_writerSettings.Encoding.WebName == ValidatingUTF16.WebName) || (this.m_writerSettings.Encoding.WebName == ValidatingBEUTF16.WebName))

    {

        XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(stream, this.m_writerSettings.Encoding, false);

        message.WriteMessage(writer);

        writer.Close();

    }

    else

    {

        XmlWriter writer = XmlWriter.Create(stream, this.m_writerSettings);

        message.WriteMessage(writer);

        writer.Close();

    }

}

Published Sun, May 6 2007 23:21 by Paulo Morgado

Filed under: , , , , , ,

Comments

# Optimizing reading for the CustomTextMessageEncoder@ Monday, May 07, 2007 3:45 PM

Continuing the improvement of the CustomTextMessageEncoder (see this and this ), this time I&#39;ll use

Paulo Morgado

# Optimizing writing for the CustomTextMessageEncoder@ Monday, May 07, 2007 5:01 PM

You've been kicked (a good thing) - Trackback from DotNetKicks.com

DotNetKicks.com

Leave a Comment

(required) 
(required) 
(optional)
(required)