MessageEncodingBindingElement 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
綁定元件,指定用於編碼訊息的訊息版本。
public ref class MessageEncodingBindingElement abstract : System::ServiceModel::Channels::BindingElement
public abstract class MessageEncodingBindingElement : System.ServiceModel.Channels.BindingElement
type MessageEncodingBindingElement = class
inherit BindingElement
Public MustInherit Class MessageEncodingBindingElement
Inherits BindingElement
- 繼承
- 衍生
範例
以下程式碼範例展示了如何實作由 衍生出 MessageEncodingBindingElement的類別:
public class CustomTextMessageBindingElement : MessageEncodingBindingElement, IWsdlExportExtension
{
private MessageVersion msgVersion;
private string mediaType;
private string encoding;
private XmlDictionaryReaderQuotas readerQuotas;
CustomTextMessageBindingElement(CustomTextMessageBindingElement binding)
: this(binding.Encoding, binding.MediaType, binding.MessageVersion)
{
this.readerQuotas = new XmlDictionaryReaderQuotas();
binding.ReaderQuotas.CopyTo(this.readerQuotas);
}
public CustomTextMessageBindingElement(string encoding, string mediaType,
MessageVersion msgVersion)
{
if (encoding == null)
throw new ArgumentNullException(nameof(encoding));
if (mediaType == null)
throw new ArgumentNullException(nameof(mediaType));
if (msgVersion == null)
throw new ArgumentNullException(nameof(msgVersion));
this.msgVersion = msgVersion;
this.mediaType = mediaType;
this.encoding = encoding;
this.readerQuotas = new XmlDictionaryReaderQuotas();
}
public CustomTextMessageBindingElement(string encoding, string mediaType)
: this(encoding, mediaType, MessageVersion.Soap11WSAddressing10)
{
}
public CustomTextMessageBindingElement(string encoding)
: this(encoding, "text/xml")
{
}
public CustomTextMessageBindingElement()
: this("UTF-8")
{
}
public override MessageVersion MessageVersion
{
get
{
return this.msgVersion;
}
set
{
if (value == null)
throw new ArgumentNullException(nameof(value));
this.msgVersion = value;
}
}
public string MediaType
{
get
{
return this.mediaType;
}
set
{
if (value == null)
throw new ArgumentNullException(nameof(value));
this.mediaType = value;
}
}
public string Encoding
{
get
{
return this.encoding;
}
set
{
if (value == null)
throw new ArgumentNullException(nameof(value));
this.encoding = value;
}
}
// This encoder does not enforces any quotas for the unsecure messages. The
// quotas are enforced for the secure portions of messages when this encoder
// is used in a binding that is configured with security.
public XmlDictionaryReaderQuotas ReaderQuotas
{
get
{
return this.readerQuotas;
}
}
#region IMessageEncodingBindingElement Members
public override MessageEncoderFactory CreateMessageEncoderFactory()
{
return new CustomTextMessageEncoderFactory(this.MediaType,
this.Encoding, this.MessageVersion);
}
#endregion
public override BindingElement Clone()
{
return new CustomTextMessageBindingElement(this);
}
public override IChannelFactory<TChannel> BuildChannelFactory<TChannel>(BindingContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
context.BindingParameters.Add(this);
return context.BuildInnerChannelFactory<TChannel>();
}
public override bool CanBuildChannelFactory<TChannel>(BindingContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
return context.CanBuildInnerChannelFactory<TChannel>();
}
public override IChannelListener<TChannel> BuildChannelListener<TChannel>(BindingContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
context.BindingParameters.Add(this);
return context.BuildInnerChannelListener<TChannel>();
}
public override bool CanBuildChannelListener<TChannel>(BindingContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
context.BindingParameters.Add(this);
return context.CanBuildInnerChannelListener<TChannel>();
}
public override T GetProperty<T>(BindingContext context)
{
if (typeof(T) == typeof(XmlDictionaryReaderQuotas))
{
return (T)(object)this.readerQuotas;
}
else
{
return base.GetProperty<T>(context);
}
}
#region IWsdlExportExtension Members
void IWsdlExportExtension.ExportContract(WsdlExporter exporter, WsdlContractConversionContext context)
{
}
void IWsdlExportExtension.ExportEndpoint(WsdlExporter exporter, WsdlEndpointConversionContext context)
{
// The MessageEncodingBindingElement is responsible for ensuring that the WSDL has the correct
// SOAP version. We can delegate to the WCF implementation of TextMessageEncodingBindingElement for this.
TextMessageEncodingBindingElement mebe = new TextMessageEncodingBindingElement();
mebe.MessageVersion = this.msgVersion;
((IWsdlExportExtension)mebe).ExportEndpoint(exporter, context);
}
#endregion
}
備註
編碼是將訊息轉換為一串位元組的過程。 解碼則是相反的過程。 Windows 通訊基金會(WCF)包含三種 SOAP 訊息的編碼方式:文字、二進位及訊息傳輸優化機制(MTOM)。
如果你想實作自訂訊息編碼器,可以使用這個類別。 若要實作您自己的自訂訊息編碼器,必須提供以下三個抽象基底類別的自訂實作:
覆寫Encoder以傳回自定義MessageEncoder的實例。 CreateMessageEncoderFactory覆蓋回傳此工廠實例的方法。
衍生自 MessageEncodingBindingElement 的任何類型都會負責更新針對服務產生的 WSDL 檔中 SOAP 系結的版本。 這是透過實作 ExportEndpoint(WsdlExporter, WsdlEndpointConversionContext) 修改產生的 WSDL 的方法來完成的。
Windows 通訊基礎(WCF)提供三種由類別 MessageEncodingBindingElement 衍生的綁定元素,可用於文字、二進位及訊息傳輸優化機制(MTOM)編碼。
TextMessageEncodingBindingElement:這是 XML 訊息中最互通,但效率最低的編碼器。 Web 服務或 Web 服務用戶端通常可以瞭解文字 XML。 不過,以文字形式傳輸大型二進位數據區塊並不有效。
BinaryMessageEncodingBindingElement: 代表用於二進位 XML 訊息的字元編碼與訊息版本控制的綁定元素。 這是編碼選項中效率最高但互通性最低的。
MtomMessageEncodingBindingElement: 代表指定訊息字元編碼及訊息版本控制的綁定元素,使用訊息傳輸優化機制(MTOM)編碼。 MTOM 是一種在 WCF 訊息中傳輸二進位數據的有效技術。 MTOM 編碼器試圖在效率與互通性之間取得平衡。 MTOM 編碼會以文字形式傳輸大部分的 XML,但對於大型的二進位數據區塊,則藉由直接傳輸 as-is而不轉換成文字,達到最佳化效果。
建構函式
| 名稱 | Description |
|---|---|
| MessageEncodingBindingElement() |
初始化 MessageEncodingBindingElement 類別的新執行個體。 |
| MessageEncodingBindingElement(MessageEncodingBindingElement) |
初始化由現有元素初始化的類別新實例 MessageEncodingBindingElement 。 |
屬性
| 名稱 | Description |
|---|---|
| MessageVersion |
當在衍生類別中覆寫時,會獲得或設定訊息版本,該版本可由訊息編碼器工廠產生的訊息編碼器處理。 |
方法
| 名稱 | Description |
|---|---|
| BuildChannelFactory<TChannel>(BindingContext) |
初始化一個通道工廠,用於從綁定上下文中產生特定類型的通道。 (繼承來源 BindingElement) |
| BuildChannelListener<TChannel>(BindingContext) |
初始化通道監聽器,以接受綁定上下文中指定類型的通道。 (繼承來源 BindingElement) |
| CanBuildChannelFactory<TChannel>(BindingContext) |
回傳一個值,指示綁定元件是否能為特定類型通道建立通道工廠。 (繼承來源 BindingElement) |
| CanBuildChannelListener<TChannel>(BindingContext) |
回傳一個值,表示綁定元件是否能為特定通道建立監聽器。 (繼承來源 BindingElement) |
| Clone() |
當在衍生類別中覆寫時,會回傳綁定元素物件的副本。 (繼承來源 BindingElement) |
| CreateMessageEncoderFactory() |
當在衍生類別中覆寫時,會建立一個用於產生訊息編碼器的工廠。 |
| Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
| GetHashCode() |
做為預設哈希函式。 (繼承來源 Object) |
| GetProperty<T>(BindingContext) |
回傳通道堆疊中適當層級所請求的類型物件(若存在)。 |
| GetType() |
取得目前實例的 Type。 (繼承來源 Object) |
| MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
| ToString() |
傳回表示目前 物件的字串。 (繼承來源 Object) |