방법: 시스템 제공 바인딩 사용자 지정

WCF(Windows Communication Foundation)에는 기본 바인딩 요소의 일부 속성을 구성할 수 있지만 모든 속성이 아닌 일부 시스템 제공 바인딩이 포함되어 있습니다. 이 항목에서는 바인딩 요소의 속성을 설정하여 사용자 지정 바인딩을 만드는 방법을 보여 줍니다.

시스템 제공 결합을 사용하지 않고 바인딩 요소를 직접 만들고 구성하는 방법에 대한 자세한 내용은 사용자 지정 바인딩을 참조하세요.

사용자 지정 바인딩 만들기 및 확장에 대한 자세한 내용은 바인딩 확장을 참조하세요.

WCF에서는 모든 바인딩이 ‘바인딩 요소’로 구성됩니다. 각 바인딩 요소는 BindingElement 클래스에서 파생됩니다. BasicHttpBinding과 같은 시스템 제공 바인딩은 자체 바인딩 요소를 만들고 구성합니다. 이 항목에서는 특히 BasicHttpBinding 클래스와 같은 바인딩에서 직접 노출되지 않는 이러한 바인딩 요소의 속성에 액세스하여 이를 변경하는 방법을 보여 줍니다.

개별 바인딩 요소는 BindingElementCollection 클래스로 표시되는 컬렉션에 포함되며, 트랜잭션 흐름, 신뢰할 수 있는 세션, 보안, 복합 이중, 단방향, 스트림 보안, 메시지 인코딩 및 전송 순서로 추가됩니다. 나열된 모든 바인딩 요소가 모든 바인딩에서 필요한 것은 아닙니다. 사용자 정의 바인딩 요소도 앞에서 설명한 동일한 순서로 이 바인딩 요소 컬렉션에 나타날 수 있습니다. 예를 들어, 사용자 정의 전송은 바인딩 요소 컬렉션의 마지막 요소여야 합니다.

BasicHttpBinding 클래스에는 다음 세 가지 바인딩 요소가 포함됩니다.

  1. HTTP 전송(메시지 수준 보안)과 함께 사용되는 AsymmetricSecurityBindingElement 클래스 또는 전송 계층에서 보안을 제공하는 경우(HTTP 전송 사용) 사용되는 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. 바인딩에서 사용자 지정 바인딩을 만들고 사용자 지정 바인딩의 속성 중 하나에서 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
    

참고 항목