다음을 통해 공유


방법: System-Provided 바인딩 사용자 지정

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

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

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

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

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

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

  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. 바인딩에서 사용자 지정 바인딩을 만들고, 사용자 지정 바인딩의 하나의 속성에서 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
    

참고하십시오