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();

    }

}

Comments are closed.