MessageEncoder.WriteMessage 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
在衍生類別中覆寫時,寫入訊息至指定的資料流或緩衝區中。
多載
WriteMessage(Message, Stream) |
在衍生類別中覆寫時,寫入訊息至指定的資料流中。 |
WriteMessage(Message, Int32, BufferManager) |
將小於指定大小的訊息寫入至位元組陣列緩衝區中。 |
WriteMessage(Message, Int32, BufferManager, Int32) |
在衍生類別中覆寫時,於位元組陣列緩衝區的指定位移處寫入小於指定大小的訊息。 |
WriteMessage(Message, Stream)
在衍生類別中覆寫時,寫入訊息至指定的資料流中。
public:
abstract void WriteMessage(System::ServiceModel::Channels::Message ^ message, System::IO::Stream ^ stream);
public abstract void WriteMessage (System.ServiceModel.Channels.Message message, System.IO.Stream stream);
abstract member WriteMessage : System.ServiceModel.Channels.Message * System.IO.Stream -> unit
Public MustOverride Sub WriteMessage (message As Message, stream As Stream)
參數
範例
下列程式碼會示範如何實作 WriteMessage(Message, Stream) 方法。
public override void WriteMessage(Message message, Stream stream)
{
XmlWriter writer = XmlWriter.Create(stream, this.writerSettings);
message.WriteMessage(writer);
writer.Close();
}
適用於
WriteMessage(Message, Int32, BufferManager)
將小於指定大小的訊息寫入至位元組陣列緩衝區中。
public:
ArraySegment<System::Byte> WriteMessage(System::ServiceModel::Channels::Message ^ message, int maxMessageSize, System::ServiceModel::Channels::BufferManager ^ bufferManager);
public ArraySegment<byte> WriteMessage (System.ServiceModel.Channels.Message message, int maxMessageSize, System.ServiceModel.Channels.BufferManager bufferManager);
member this.WriteMessage : System.ServiceModel.Channels.Message * int * System.ServiceModel.Channels.BufferManager -> ArraySegment<byte>
Public Function WriteMessage (message As Message, maxMessageSize As Integer, bufferManager As BufferManager) As ArraySegment(Of Byte)
參數
- maxMessageSize
- Int32
可寫入的訊息大小上限。
- bufferManager
- BufferManager
BufferManager,管理在其中寫入訊息的緩衝區。
傳回
ArraySegment<T> 型別的 byte
,提供序列化訊息的緩衝區。
備註
這個方法會呼叫 WriteMessage(Message, Int32, BufferManager, Int32) = 0 的抽象方法 messageOffset
。
適用於
WriteMessage(Message, Int32, BufferManager, Int32)
在衍生類別中覆寫時,於位元組陣列緩衝區的指定位移處寫入小於指定大小的訊息。
public:
abstract ArraySegment<System::Byte> WriteMessage(System::ServiceModel::Channels::Message ^ message, int maxMessageSize, System::ServiceModel::Channels::BufferManager ^ bufferManager, int messageOffset);
public abstract ArraySegment<byte> WriteMessage (System.ServiceModel.Channels.Message message, int maxMessageSize, System.ServiceModel.Channels.BufferManager bufferManager, int messageOffset);
abstract member WriteMessage : System.ServiceModel.Channels.Message * int * System.ServiceModel.Channels.BufferManager * int -> ArraySegment<byte>
Public MustOverride Function WriteMessage (message As Message, maxMessageSize As Integer, bufferManager As BufferManager, messageOffset As Integer) As ArraySegment(Of Byte)
參數
- maxMessageSize
- Int32
可寫入的訊息大小上限。
- bufferManager
- BufferManager
BufferManager,管理在其中寫入訊息的緩衝區。
- messageOffset
- Int32
從提供緩衝區的位元組陣列開頭開始的區段位移。
傳回
ArraySegment<T> 型別的 byte
,提供序列化訊息的緩衝區。
範例
下列程式碼會示範如何實作 WriteMessage(Message, Int32, BufferManager, Int32) 方法。
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;
}
備註
這個方法是由 WriteMessage(Message, Int32, BufferManager, Int32) 所呼叫。