次の方法で共有


方法: System-Provided バインドをカスタマイズする

Windows Communication Foundation (WCF) には、基になるバインディング要素のプロパティの一部を構成できる、システムが提供するいくつかのバインディングが含まれていますが、すべてのプロパティは構成できません。 このトピックでは、バインディング要素のプロパティを設定してカスタム バインドを作成する方法について説明します。

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

カスタム バインドの作成と拡張の詳細については、「バインドの拡張」 参照してください。

WCF では、すべてのバインディングは バインド要素で構成されます。 各バインド要素は、 BindingElement クラスから派生します。 BasicHttpBindingなどのシステム提供のバインディングは、独自のバインド要素を作成して構成します。 このトピックでは、これらのバインド要素のプロパティにアクセスして変更する方法について説明します。このプロパティはバインドで直接公開されません。具体的には、 BasicHttpBinding クラスです。

個々のバインド要素は、 BindingElementCollection クラスによって表されるコレクションに含まれており、トランザクション フロー、Reliable Session、Security、Composite Duplex、One-Way、Stream Security、Message Encoding、Transport という順序で追加されます。 一覧表示されているすべてのバインド要素がすべてのバインドで必要なわけではないことに注意してください。 ユーザー定義のバインド要素は、このバインド要素コレクションにも含まれる可能性があり、前述と同じ順序で表示する必要があります。 たとえば、ユーザー定義トランスポートはバインディング要素コレクションの最後の要素である必要があります。

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

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

  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 = {httpElement.KeepAliveEnabled}");
            httpElement.KeepAliveEnabled = false;
            Console.WriteLine($"\tAfter:  HttpTransportBindingElement.KeepAliveEnabled = {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
    

こちらも参照ください