MessageEncoder Class

 

The encoder is the component that is used to write messages to a stream and to read messages from a stream.

Namespace:   System.ServiceModel.Channels
Assembly:  System.ServiceModel (in System.ServiceModel.dll)

System.Object
  System.ServiceModel.Channels.MessageEncoder

[<AbstractClass>]
type MessageEncoder = class end

NameDescription
System_CAPS_protmethodMessageEncoder()

Initializes a new instance of the MessageEncoder class.

NameDescription
System_CAPS_pubpropertyContentType

When overridden in a derived class, gets the MIME content type used by the encoder.

System_CAPS_pubpropertyMediaType

When overridden in a derived class, gets the media type value that is used by the encoder.

System_CAPS_pubpropertyMessageVersion

When overridden in a derived class, gets the message version value that is used by the encoder.

NameDescription
System_CAPS_pubmethodBeginWriteMessage(Message, Stream, AsyncCallback, Object)

Starts the writing of message for the message encoder with specified message, stream, callback and state.

System_CAPS_pubmethodEndWriteMessage(IAsyncResult)

Ends the writing of messages for the message encoder.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetProperty<'T>()

Returns a typed object requested, if present, from the appropriate layer in the channel stack.

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodIsContentTypeSupported(String)

Returns a value that indicates whether a specified message-level content-type value is supported by the message encoder.

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_pubmethodReadMessage(ArraySegment<Byte>, BufferManager)

When overridden in a derived class, reads a message from a specified buffer.

System_CAPS_pubmethodReadMessage(ArraySegment<Byte>, BufferManager, String)

When overridden in a derived class, reads a message from a specified stream.

System_CAPS_pubmethodReadMessage(Stream, Int32)

When overridden in a derived class, reads a message from a specified stream.

System_CAPS_pubmethodReadMessage(Stream, Int32, String)

When overridden in a derived class, reads a message from a specified stream.

System_CAPS_pubmethodToString()

Returns the content type that is used by the message encoder.(Overrides Object.ToString().)

System_CAPS_pubmethodWriteMessage(Message, Int32, BufferManager)

Writes a message less than a specified size to a byte array buffer.

System_CAPS_pubmethodWriteMessage(Message, Int32, BufferManager, Int32)

When overridden in a derived class, writes a message of less than a specified size to a byte array buffer at the specified offset.

System_CAPS_pubmethodWriteMessage(Message, Stream)

When overridden in a derived class, writes a message to a specified stream.

MessageEncoder is a base class that provides implementations that support a Multipurpose Internet Mail Extensions (MIME) content type and message version, and defines the interface for the serialization and deserialization of messages according to that content type. Use it as a base class for writing your own custom encoder.

Use this class if you want to implement a custom message encoder. To implement your own custom message encoder, you must provide custom implementations of the following abstract base classes:

Override the Encoder to return an instance of your custom MessageEncoder. Then wire up your custom MessageEncoderFactory to the binding element stack used to configure the service or client by overriding the CreateMessageEncoderFactory method to return an instance of this factory.

The task of converting between the in-memory representation of a message and an XML Information Set (Infoset) representation that can be written to a stream is encapsulated within the MessageEncoder class, which most commonly serves as a factory for XML readers and XML writers that support specific types of XML encodings.

The key methods on MessageEncoder are WriteMessage and ReadMessage. WriteMessage takes a Message object and writes it into a Stream object. ReadMessage takes a Stream object and a maximum header size and returns a Message object.

The following code shows an example of a class that is derived from MessageEncoder.

public class CustomTextMessageEncoder : MessageEncoder
{
    private CustomTextMessageEncoderFactory factory;
    private XmlWriterSettings writerSettings;
    private string contentType;

    public CustomTextMessageEncoder(CustomTextMessageEncoderFactory factory)
    {
        this.factory = factory;

        this.writerSettings = new XmlWriterSettings();
        this.writerSettings.Encoding = Encoding.GetEncoding(factory.CharSet);
        this.contentType = string.Format("{0}; charset={1}", 
            this.factory.MediaType, this.writerSettings.Encoding.HeaderName);
    }

    public override string ContentType
    {
        get
        {
            return this.contentType;
        }
    }

    public override string MediaType
    {
        get 
        {
            return factory.MediaType;
        }
    }

    public override MessageVersion MessageVersion
    {
        get 
        {
            return this.factory.MessageVersion;
        }
    }

    public override bool IsContentTypeSupported(string contentType) 
    { 
        if (base.IsContentTypeSupported(contentType)) 
        { 
            return true; 
        } 
        if (contentType.Length == this.MediaType.Length) 
        { 
            return contentType.Equals(this.MediaType, StringComparison.OrdinalIgnoreCase); 
        } 
        else 
        { 
            if (contentType.StartsWith(this.MediaType, StringComparison.OrdinalIgnoreCase) 
                && (contentType[this.MediaType.Length] == ';')) 
            { 
                return true; 
            } 
        } 
        return false; 
    }

    public override Message ReadMessage(ArraySegment<byte> buffer, BufferManager bufferManager, string contentType)
    {   
        byte[] msgContents = new byte[buffer.Count];
        Array.Copy(buffer.Array, buffer.Offset, msgContents, 0, msgContents.Length);
        bufferManager.ReturnBuffer(buffer.Array);

        MemoryStream stream = new MemoryStream(msgContents);
        return ReadMessage(stream, int.MaxValue);
    }

    public override Message ReadMessage(Stream stream, int maxSizeOfHeaders, string contentType)
    {
        XmlReader reader = XmlReader.Create(stream);
        return Message.CreateMessage(reader, maxSizeOfHeaders, this.MessageVersion);
    }

    public override ArraySegment<byte> WriteMessage(Message message, int maxMessageSize, BufferManager bufferManager, int messageOffset)
    {
        MemoryStream stream = new MemoryStream();
        XmlWriter writer = XmlWriter.Create(stream, this.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)
    {
        XmlWriter writer = XmlWriter.Create(stream, this.writerSettings);
        message.WriteMessage(writer);
        writer.Close();
    }

Universal Windows Platform
Available since 8
.NET Framework
Available since 3.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show: