次の方法で共有


MessageEncodingBindingElement クラス

定義

メッセージのエンコードに使用されるメッセージのバージョンを指定するバインディング要素。

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
派生

次のコード例は、 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 Communication Foundation (WCF) には、SOAP メッセージの 3 種類のエンコードが含まれています。テキスト、バイナリ、メッセージ送信の最適化メカニズム (MTOM) です。

カスタム メッセージ エンコーダーを実装する場合は、このクラスを使用します。 独自のカスタム メッセージ エンコーダーを実装するには、次の 3 つの抽象基本クラスのカスタム実装を提供する必要があります。

Encoderをオーバーライドして、カスタム MessageEncoderのインスタンスを返します。 CreateMessageEncoderFactory メソッドをオーバーライドして、このファクトリのインスタンスを返します。

MessageEncodingBindingElementから派生するすべての型は、サービス用に生成された WSDL ドキュメント内の SOAP バインディングのバージョンを更新します。 これを行うには、生成された WSDL を変更する ExportEndpoint(WsdlExporter, WsdlEndpointConversionContext) メソッドを実装します。

Windows Communication Foundation (WCF) には、テキスト、バイナリ、およびメッセージ送信最適化メカニズム (MTOM) エンコードを提供できる、 MessageEncodingBindingElement クラスから派生した 3 種類のバインド要素が用意されています。

  • TextMessageEncodingBindingElement: 最も相互運用可能ですが、XML メッセージの効率は最も低いエンコーダーです。 Web サービスまたは Web サービス クライアントは、通常、テキスト XML を理解できます。 ただし、バイナリ データの大きなブロックをテキストとして送信することは効率的ではありません。

  • BinaryMessageEncodingBindingElement: バイナリ ベースの XML メッセージに使用される文字エンコードとメッセージのバージョン管理を指定するバインディング要素を表します。 これは最も効率的ですが、エンコード オプションの相互運用性は最も低いです。

  • MtomMessageEncodingBindingElement: メッセージ転送最適化メカニズム (MTOM) エンコードを使用してメッセージに使用される文字エンコードとメッセージのバージョン管理を指定するバインディング要素を表します。 MTOM は、WCF メッセージでバイナリ データを送信するための効率的なテクノロジです。 MTOM エンコーダーは、効率と相互運用性のバランスを取ろうとします。 MTOM エンコードは、ほとんどの XML をテキスト形式で送信しますが、テキストに変換することなく、as-is送信することでバイナリ データの大きなブロックを最適化します。

コンストラクター

名前 説明
MessageEncodingBindingElement()

MessageEncodingBindingElement クラスの新しいインスタンスを初期化します。

MessageEncodingBindingElement(MessageEncodingBindingElement)

既存の要素から初期化された MessageEncodingBindingElement クラスの新しいインスタンスを初期化します。

プロパティ

名前 説明
MessageVersion

派生クラスでオーバーライドされると、メッセージ エンコーダー ファクトリによって生成されたメッセージ エンコーダーによって処理できるメッセージ バージョンを取得または設定します。

メソッド

名前 説明
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)

適用対象