MessageEncoder MessageEncoder MessageEncoder MessageEncoder Class

定義

編碼器是用來將訊息寫入資料流及從資料流讀取訊息的元件。The encoder is the component that is used to write messages to a stream and to read messages from a stream.

public ref class MessageEncoder abstract
public abstract class MessageEncoder
type MessageEncoder = class
Public MustInherit Class MessageEncoder
繼承
MessageEncoderMessageEncoderMessageEncoderMessageEncoder

範例

下列程式碼會示範衍生自 MessageEncoder 的類別範例。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();
    }

備註

MessageEncoder 是一種基底類別 (Base Class),提供支援多用途網際網路郵件延伸標準 (MIME) 內容類型和訊息版本的實作,並根據該內容類型定義序列化和還原序列化訊息的介面。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:

覆寫 Encoder,以傳回自訂 MessageEncoder 的執行個體。Override the Encoder to return an instance of your custom MessageEncoder. 然後讓自訂 MessageEncoderFactory 連接用來設定服務或用戶端的繫結項目堆疊,其方式是覆寫 CreateMessageEncoderFactory 方法來傳回此處理站的執行個體。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.

記憶體內的訊息表示以及可寫入資料流的 XML 資訊集 (Infoset) 表示兩者間的轉換工作是封裝在 MessageEncoder 類別中,這個類別最常當做可支援特定類型 XML 編碼之 XML 讀取器和 XML 寫入器的處理站使用。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.

MessageEncoder 上的主要方法是 WriteMessageReadMessageThe key methods on MessageEncoder are WriteMessage and ReadMessage. WriteMessage 會接受 Message 物件,並將它寫入至 Stream 物件。WriteMessage takes a Message object and writes it into a Stream object. ReadMessage 會接受 Stream 物件和最大標頭大小,然後傳回 Message 物件。ReadMessage takes a Stream object and a maximum header size and returns a Message object.

建構函式

MessageEncoder() MessageEncoder() MessageEncoder() MessageEncoder()

初始化 MessageEncoder 類別的新執行個體。Initializes a new instance of the MessageEncoder class.

屬性

ContentType ContentType ContentType ContentType

在衍生類別 (Derived Class) 中覆寫時,會取得編碼器使用的 MIME 內容類型。When overridden in a derived class, gets the MIME content type used by the encoder.

MediaType MediaType MediaType MediaType

在衍生類別中覆寫時,會取得編碼器使用的媒體類型值。When overridden in a derived class, gets the media type value that is used by the encoder.

MessageVersion MessageVersion MessageVersion MessageVersion

在衍生類別中覆寫時,會取得編碼器使用的訊息版本值。When overridden in a derived class, gets the message version value that is used by the encoder.

方法

BeginWriteMessage(Message, Stream, AsyncCallback, Object) BeginWriteMessage(Message, Stream, AsyncCallback, Object) BeginWriteMessage(Message, Stream, AsyncCallback, Object) BeginWriteMessage(Message, Stream, AsyncCallback, Object)

開始以指定的訊息、資料流、回呼及狀態寫入訊息編碼器的訊息。Starts the writing of message for the message encoder with specified message, stream, callback and state.

EndWriteMessage(IAsyncResult) EndWriteMessage(IAsyncResult) EndWriteMessage(IAsyncResult) EndWriteMessage(IAsyncResult)

結束寫入訊息編碼器的訊息。Ends the writing of messages for the message encoder.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetProperty<T>() GetProperty<T>() GetProperty<T>() GetProperty<T>()

從通道堆疊的適當層次中,傳回要求的型別物件 (如果有)。Returns a typed object requested, if present, from the appropriate layer in the channel stack.

GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
IsContentTypeSupported(String) IsContentTypeSupported(String) IsContentTypeSupported(String) IsContentTypeSupported(String)

傳回值,這個值表示訊息編碼器是否支援指定的訊息層級內容類型值。Returns a value that indicates whether a specified message-level content-type value is supported by the message encoder.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
ReadMessage(ArraySegment<Byte>, BufferManager) ReadMessage(ArraySegment<Byte>, BufferManager) ReadMessage(ArraySegment<Byte>, BufferManager) ReadMessage(ArraySegment<Byte>, BufferManager)

在衍生類別中覆寫時,從指定的緩衝區讀取訊息。When overridden in a derived class, reads a message from a specified buffer.

ReadMessage(ArraySegment<Byte>, BufferManager, String) ReadMessage(ArraySegment<Byte>, BufferManager, String) ReadMessage(ArraySegment<Byte>, BufferManager, String) ReadMessage(ArraySegment<Byte>, BufferManager, String)

在衍生類別中覆寫時,從指定的資料流讀取訊息。When overridden in a derived class, reads a message from a specified stream.

ReadMessage(Stream, Int32) ReadMessage(Stream, Int32) ReadMessage(Stream, Int32) ReadMessage(Stream, Int32)

在衍生類別中覆寫時,從指定的資料流讀取訊息。When overridden in a derived class, reads a message from a specified stream.

ReadMessage(Stream, Int32, String) ReadMessage(Stream, Int32, String) ReadMessage(Stream, Int32, String) ReadMessage(Stream, Int32, String)

在衍生類別中覆寫時,從指定的資料流讀取訊息。When overridden in a derived class, reads a message from a specified stream.

ToString() ToString() ToString() ToString()

傳回訊息編碼器使用的內容類型。Returns the content type that is used by the message encoder.

WriteMessage(Message, Int32, BufferManager) WriteMessage(Message, Int32, BufferManager) WriteMessage(Message, Int32, BufferManager) WriteMessage(Message, Int32, BufferManager)

將小於指定大小的訊息寫入至位元組陣列緩衝區中。Writes a message less than a specified size to a byte array buffer.

WriteMessage(Message, Int32, BufferManager, Int32) WriteMessage(Message, Int32, BufferManager, Int32) WriteMessage(Message, Int32, BufferManager, Int32) WriteMessage(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.

WriteMessage(Message, Stream) WriteMessage(Message, Stream) WriteMessage(Message, Stream) WriteMessage(Message, Stream)

在衍生類別中覆寫時,寫入訊息至指定的資料流中。When overridden in a derived class, writes a message to a specified stream.

適用於