方法: システム指定のバインディングをカスタマイズする

Windows Communication Foundation (WCF) には、システム指定のバインディングがいくつか含まれています。これらのバインディングを使用して、基になるバインド要素の一部のプロパティを構成できますが、すべてのプロパティを構成できるとは限りません。 ここでは、バインド要素のプロパティを設定してカスタム バインドを作成する方法を示します。

システム指定のバインディングを使用せずに、バインド要素を直接作成および構成する方法の詳細については、「カスタム バインディング」を参照してください。

カスタム バインディングを作成および拡張する方法の詳細については、「バインディングの拡張」を参照してください。

WCF では、すべてのバインディングが複数のバインディング要素で構成されています。 各バインド要素は BindingElement クラスから派生します。 BasicHttpBinding などのシステム指定のバインディングでは、独自のバインド要素が作成され構成されます。 ここでは、バインディングに直接公開されないこのバインド要素 (具体的には BasicHttpBinding クラス) のプロパティにアクセスして変更する方法を示します。

個別のバインド要素は BindingElementCollection クラスで表されるコレクションに格納し、トランザクション フロー、信頼できるセッション、セキュリティ、複合二重、一方向、ストリーム セキュリティ、メッセージ エンコーディング、トランスポートの順に追加します。 どのバインディングでも、これらすべてのバインド要素が必要になるとは限りません。 ユーザー定義のバインド要素も、このバインド要素のコレクションに表示されますが、前述の順序で表示される必要があります。 たとえば、ユーザー定義のトランスポートは、バインド要素コレクションの最後の要素となる必要があります。

BasicHttpBinding クラスには、次の 3 つのバインド要素が含まれています。

  1. オプションのセキュリティ バインド要素。HTTP トランスポートで使用される AsymmetricSecurityBindingElement クラス (メッセージ レベル セキュリティ)、またはトランスポート層がセキュリティを提供する場合 (HTTPS トランスポート) に使用される TransportSecurityBindingElement

  2. 必須のメッセージ エンコーダー バインド要素。TextMessageEncodingBindingElement または MtomMessageEncodingBindingElement

  3. 必須のトランスポート バインド要素。HttpTransportBindingElement または HttpsTransportBindingElement

この例では、バインディングのインスタンスを作成し、そのインスタンスからカスタム バインディングを生成します。次にカスタム バインディング内のバインディング要素を調べ、HTTP バインディング要素が見つかった場合は、その KeepAliveEnabled プロパティを false に設定します。 KeepAliveEnabled プロパティは、BasicHttpBinding に直接公開されていないので、カスタム バインドを作成し、バインド要素まで移動して、このプロパティを設定する必要があります。

システム標準のバインディングを変更するには

  1. BasicHttpBinding クラスのインスタンスを作成し、そのセキュリティ モードをメッセージ レベルに設定します。

    //  Create an instance of the T:System.ServiceModel.BasicHttpBinding
    //  class and set its security mode to message-level security.
    BasicHttpBinding binding = new BasicHttpBinding();
    binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.Certificate;
    binding.Security.Mode = BasicHttpSecurityMode.Message;
    
    '  Create an instance of the T:System.ServiceModel.BasicHttpBinding 
    '  class and set its security mode to message-level security.
    Dim binding As New BasicHttpBinding()
    With binding.Security
        .Message.ClientCredentialType = BasicHttpMessageCredentialType.Certificate
        .Mode = BasicHttpSecurityMode.Message
    End With
    
  2. バインディングからカスタム バインドを作成し、そのカスタム バインドのプロパティの 1 つから BindingElementCollection クラスを作成します。

    //  Create a custom binding from the binding
    CustomBinding cb = new CustomBinding(binding);
    //  Get the BindingElementCollection from this custom binding
    BindingElementCollection bec = cb.Elements();
    
    '  Create a custom binding from the binding 
    Dim cb As New CustomBinding(binding)
    '  Get the BindingElementCollection from this custom binding 
    Dim bec = cb.Elements
    
  3. BindingElementCollection クラスをループして HttpTransportBindingElement クラスが見つかったら、その KeepAliveEnabled プロパティを false に設定します。

    //  Loop through the collection, and when you find the HTTP binding element
    //  set its KeepAliveEnabled property to false
    foreach (BindingElement be in bec)
    {
        Type thisType = be.GetType();
        Console.WriteLine(thisType);
        if (be is HttpTransportBindingElement)
        {
            HttpTransportBindingElement httpElement = (HttpTransportBindingElement)be;
            Console.WriteLine("\tBefore: HttpTransportBindingElement.KeepAliveEnabled = {0}", httpElement.KeepAliveEnabled);
            httpElement.KeepAliveEnabled = false;
            Console.WriteLine("\tAfter:  HttpTransportBindingElement.KeepAliveEnabled = {0}", httpElement.KeepAliveEnabled);
        }
    }
    
    '  Loop through the collection, and when you find the HTTP binding element
    '  set its KeepAliveEnabled property to false
    For Each be In bec
        Dim thisType = be.GetType()
        Console.WriteLine(thisType)
        If TypeOf be Is HttpTransportBindingElement Then
            Dim httpElement As HttpTransportBindingElement = CType(be, HttpTransportBindingElement)
            Console.WriteLine(Constants.vbTab & "Before: HttpTransportBindingElement.KeepAliveEnabled = {0}", httpElement.KeepAliveEnabled)
            httpElement.KeepAliveEnabled = False
            Console.WriteLine(vbTab & "After:  HttpTransportBindingElement.KeepAliveEnabled = {0}", httpElement.KeepAliveEnabled)
        End If
    Next be
    

関連項目