다음을 통해 공유


클라이언트 동작 구성

WCF(Windows Communication Foundation)에서는 클라이언트 응용 프로그램 구성 파일의 <behavior> 섹션에 정의된 동작 구성을 참조하거나 호출 응용 프로그램에서 프로그래밍 방식으로 동작을 구성합니다. 이 항목에서는 두 접근 방식 모두에 대해 설명합니다.

구성 파일을 사용할 경우 동작 구성은 구성 설정의 명명된 컬렉션입니다. 각 동작 구성의 이름은 고유해야 합니다. 이 문자열은 끝점을 동작에 연결하는 끝점 구성의 behaviorConfiguration 특성에 사용됩니다.

예제

다음 구성 코드에서는 myBehavior라는 동작을 정의합니다. 클라이언트 끝점은 behaviorConfiguration 특성에서 이 동작을 참조합니다.

<configuration>
    <system.serviceModel>
        <behaviors>
            <endpointBehaviors>
                <behavior name="myBehavior">
                    <clientVia />
                </behavior>
            </endpointBehaviors>
        </behaviors>
        <bindings>
            <basicHttpBinding>
                <binding name="myBinding" maxReceivedMessageSize="10000" />
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="myAddress" binding="basicHttpBinding" bindingConfiguration="myBinding" behaviorConfiguration="myBehavior" contract="myContract" />
        </client>
    </system.serviceModel>
</configuration>

프로그래밍 방식으로 동작 사용

클라이언트를 열기 전에 WCF(Windows Communication Foundation) 클라이언트 개체 또는 클라이언트 채널 팩터리 개체에서 적절한 Behaviors 속성을 찾아 프로그래밍 방식으로 동작을 구성 또는 삽입할 수도 있습니다.

예제

다음 코드 예제에서는 채널 개체를 만들기 전에 Endpoint 속성에서 반환된 ServiceEndpointBehaviors 속성에 액세스하여 프로그래밍 방식으로 동작을 삽입하는 방법을 보여 줍니다.

Public Class Client
  Public Shared Sub Main()
    Try
      ' Picks up configuration from the config file.
      Dim factory As New ChannelFactory(Of ISampleServiceChannel)("WSHttpBinding_ISampleService")

      ' Add the client side behavior programmatically to all created channels.
      factory.Endpoint.Behaviors.Add(New EndpointBehaviorMessageInspector())

      Dim wcfClientChannel As ISampleServiceChannel = factory.CreateChannel()

      ' Making calls.
      Console.WriteLine("Enter the greeting to send: ")
            Dim greeting As String = Console.ReadLine()
      Console.WriteLine("The service responded: " & wcfClientChannel.SampleMethod(greeting))

      Console.WriteLine("Press ENTER to exit:")
      Console.ReadLine()

      ' Done with service. 
      wcfClientChannel.Close()
      Console.WriteLine("Done!")
    Catch timeProblem As TimeoutException
      Console.WriteLine("The service operation timed out. " & timeProblem.Message)
      Console.Read()
    Catch fault As FaultException(Of SampleFault)
      Console.WriteLine("SampleFault fault occurred: {0}", fault.Detail.FaultMessage)
      Console.Read()
    Catch commProblem As CommunicationException
      Console.WriteLine("There was a communication problem. " & commProblem.Message)
      Console.Read()
    End Try
  End Sub

참고 항목

기타 리소스

<behaviors>