클라이언트 런타임 동작 지정

WCF(Windows Communication Foundation) 서비스와 같은 WCF(Windows Communication Foundation) 클라이언트는 클라이언트 애플리케이션에 맞게 런타임 동작을 수정하도록 구성할 수 있습니다. 세 가지 특성을 사용하여 클라이언트 런타임 동작을 지정할 수 있습니다. 이중 클라이언트 콜백 개체는 CallbackBehaviorAttributeCallbackDebugBehavior 특성을 사용하여 런타임 동작을 수정할 수 있습니다. 다른 특성인 ClientViaBehavior는 논리 대상과 직접 네트워크 대상을 구분하는 데 사용할 수 있습니다. 또한 이중 클라이언트 콜백 형식은 서비스측 동작 중 일부를 사용할 수 있습니다. 자세한 내용은 서비스 런타임 동작 지정을 참조하세요.

CallbackBehaviorAttribute 사용

CallbackBehaviorAttribute 클래스를 사용하여 클라이언트 애플리케이션에서 콜백 계약 구현의 실행 동작을 구성하거나 확장할 수 있습니다. 이 특성은 인스턴스 동작과 트랜잭션 설정을 제외하고 ServiceBehaviorAttribute 클래스와 유사한 기능을 콜백 클래스에 대해 수행합니다.

CallbackBehaviorAttribute 클래스는 콜백 계약을 구현하는 클래스에 적용해야 합니다. 비이중 계약 구현에 적용하면 런타임에 InvalidOperationException 예외가 throw됩니다. 다음 코드 예제에서는 CallbackBehaviorAttribute 개체를 사용하여 마샬링할 스레드를 확인하고, SynchronizationContext 속성을 사용하여 메시지 유효성 검사를 적용하고, ValidateMustUnderstand 속성을 사용하여 디버깅 목적으로 서비스에 대한 IncludeExceptionDetailInFaults 개체로 예외를 반환하는 콜백 개체의 FaultException 클래스를 보여 줍니다.

using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Threading;

namespace Microsoft.WCF.Documentation
{
  [CallbackBehaviorAttribute(
   IncludeExceptionDetailInFaults= true,
    UseSynchronizationContext=true,
    ValidateMustUnderstand=true
  )]
  public class Client : SampleDuplexHelloCallback
  {
    AutoResetEvent waitHandle;

    public Client()
    {
      waitHandle = new AutoResetEvent(false);
    }

    public void Run()
    {
      // Picks up configuration from the configuration file.
      SampleDuplexHelloClient wcfClient
        = new SampleDuplexHelloClient(new InstanceContext(this), "WSDualHttpBinding_SampleDuplexHello");
      try
      {
        Console.ForegroundColor = ConsoleColor.White;
        Console.WriteLine("Enter a greeting to send and press ENTER: ");
        Console.Write(">>> ");
        Console.ForegroundColor = ConsoleColor.Green;
        string greeting = Console.ReadLine();
        Console.ForegroundColor = ConsoleColor.White;
        Console.WriteLine("Called service with: \r\n\t" + greeting);
        wcfClient.Hello(greeting);
        Console.WriteLine("Execution passes service call and moves to the WaitHandle.");
        this.waitHandle.WaitOne();
        Console.ForegroundColor = ConsoleColor.Blue;
        Console.WriteLine("Set was called.");
        Console.Write("Press ");
        Console.ForegroundColor = ConsoleColor.Red;
        Console.Write("ENTER");
        Console.ForegroundColor = ConsoleColor.Blue;
        Console.Write(" to exit...");
        Console.ReadLine();
      }
      catch (TimeoutException timeProblem)
      {
        Console.WriteLine("The service operation timed out. " + timeProblem.Message);
        Console.ReadLine();
      }
      catch (CommunicationException commProblem)
      {
        Console.WriteLine("There was a communication problem. " + commProblem.Message);
        Console.ReadLine();
      }
    }
    public static void Main()
    {
      Client client = new Client();
      client.Run();
    }

    public void Reply(string response)
    {
      Console.WriteLine("Received output.");
      Console.WriteLine("\r\n\t" + response);
      this.waitHandle.Set();
    }
  }
}

Imports System.ServiceModel
Imports System.ServiceModel.Channels
Imports System.Threading

Namespace Microsoft.WCF.Documentation
    <CallbackBehaviorAttribute(IncludeExceptionDetailInFaults:=True, UseSynchronizationContext:=True, ValidateMustUnderstand:=True)> _
    Public Class Client
        Implements SampleDuplexHelloCallback
        Private waitHandle As AutoResetEvent

        Public Sub New()
            waitHandle = New AutoResetEvent(False)
        End Sub

        Public Sub Run()
            ' Picks up configuration from the configuration file.
            Dim wcfClient As New SampleDuplexHelloClient(New InstanceContext(Me), "WSDualHttpBinding_SampleDuplexHello")
            Try
                Console.ForegroundColor = ConsoleColor.White
                Console.WriteLine("Enter a greeting to send and press ENTER: ")
                Console.Write(">>> ")
                Console.ForegroundColor = ConsoleColor.Green
                Dim greeting As String = Console.ReadLine()
                Console.ForegroundColor = ConsoleColor.White
                Console.WriteLine("Called service with: " & Constants.vbCrLf & Constants.vbTab & greeting)
                wcfClient.Hello(greeting)
                Console.WriteLine("Execution passes service call and moves to the WaitHandle.")
                Me.waitHandle.WaitOne()
                Console.ForegroundColor = ConsoleColor.Blue
                Console.WriteLine("Set was called.")
                Console.Write("Press ")
                Console.ForegroundColor = ConsoleColor.Red
                Console.Write("ENTER")
                Console.ForegroundColor = ConsoleColor.Blue
                Console.Write(" to exit...")
                Console.ReadLine()
            Catch timeProblem As TimeoutException
                Console.WriteLine("The service operation timed out. " & timeProblem.Message)
                Console.ReadLine()
            Catch commProblem As CommunicationException
                Console.WriteLine("There was a communication problem. " & commProblem.Message)
                Console.ReadLine()
            End Try
        End Sub
        Public Shared Sub Main()
            Dim client As New Client()
            client.Run()
        End Sub

        Public Sub Reply(ByVal response As String) Implements SampleDuplexHelloCallback.Reply
            Console.WriteLine("Received output.")
            Console.WriteLine(Constants.vbCrLf & Constants.vbTab & response)
            Me.waitHandle.Set()
        End Sub
    End Class
End Namespace

CallbackDebugBehavior를 사용하여 관리되는 예외 정보의 흐름 활성화

프로그래밍 방식으로 또는 애플리케이션 구성 파일에서 IncludeExceptionDetailInFaults 속성을 true로 설정하여 디버깅 목적으로 클라이언트 콜백 개체에 있는 관리되는 예외 정보 흐름을 다시 서비스로 이동되게 할 수 있습니다.

관리형 예외 정보를 서비스에 반환하면 예외 정보가 내부 클라이언트 구현 정보를 노출하여 권한이 없는 서비스에서 사용할 수 있으므로 보안상 위험할 수 있습니다. 또한 CallbackDebugBehavior 속성을 프로그래밍 방식으로 설정할 수도 있지만 배포 시 IncludeExceptionDetailInFaults를 사용하지 않도록 설정하는 것을 잊어버리기 쉽습니다.

관련된 보안 문제로 인해 다음을 수행하는 것이 좋습니다.

  • 애플리케이션 구성 파일을 사용하여 IncludeExceptionDetailInFaults 속성 값을 true로 설정합니다.

  • 제어된 디버깅 시나리오에서만 이 작업을 수행합니다.

다음 코드 예제에서는 클라이언트 콜백 개체의 관리되는 예외 정보를 SOAP 메시지에 반환하도록 WCF에 지시하는 클라이언트 구성 파일을 보여 줍니다.

  <client>
      <endpoint 
        address="http://localhost:8080/DuplexHello" 
        binding="wsDualHttpBinding"
        bindingConfiguration="WSDualHttpBinding_SampleDuplexHello"
        contract="SampleDuplexHello" 
        name="WSDualHttpBinding_SampleDuplexHello"
        behaviorConfiguration="enableCallbackDebug">
      </endpoint>
  </client>
<behaviors>
  <endpointBehaviors>
    <behavior name="enableCallbackDebug">
      <callbackDebug includeExceptionDetailInFaults="true"/>
    </behavior>
  </endpointBehaviors>
</behaviors>

ClientViaBehavior 동작 사용

ClientViaBehavior 동작을 사용하여 전송 채널을 만들어야 하는 Uniform Resource Identifier를 지정할 수 있습니다. 직접 네트워크 대상이 메시지의 의도된 프로세서가 아닌 경우 이 동작을 사용합니다. 이 경우 호출 애플리케이션에서 최종 대상을 알 필요가 없거나 대상 Via 헤더가 주소가 아닌 경우에 다중 홉 대화가 가능합니다.

참고 항목