다음을 통해 공유


IClientMessageInspector.BeforeSendRequest(Message, IClientChannel) 메서드

정의

요청 메시지가 서비스에 전달되기 전에 메시지를 검사하거나 수정할 수 있습니다.

public:
 System::Object ^ BeforeSendRequest(System::ServiceModel::Channels::Message ^ % request, System::ServiceModel::IClientChannel ^ channel);
public object BeforeSendRequest (ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel);
abstract member BeforeSendRequest : Message * System.ServiceModel.IClientChannel -> obj
Public Function BeforeSendRequest (ByRef request As Message, channel As IClientChannel) As Object

매개 변수

request
Message

서비스에 보낼 메시지입니다.

channel
IClientChannel

WCF 클라이언트 개체 채널입니다.

반환

Object

AfterReceiveReply(Message, Object) 메서드의 correlationState 인수로 반환되는 개체입니다. 상관 관계 상태를 사용하지 않으면 null입니다.

가장 좋은 방법은 이 개체를 Guid로 만들어 동일한 두 correlationState 개체가 존재하지 않도록 하는 것입니다.

예제

다음 코드 예제에서는 구현이 호출 될 때 콘솔에 문자열을 쓰는 구현을 보여 줍니다.

#region IClientMessageInspector Members
public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
{
  Console.WriteLine("IClientMessageInspector.AfterReceiveReply called.");
  Console.WriteLine("Message: {0}", reply.ToString());
}

public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel)
{
  Console.WriteLine("IClientMessageInspector.BeforeSendRequest called.");
  return null;
}
#Region "IClientMessageInspector Members"
    Public Sub AfterReceiveReply(ByRef reply As System.ServiceModel.Channels.Message, _
                       ByVal correlationState As Object) Implements IClientMessageInspector.AfterReceiveReply
        Console.WriteLine("IClientMessageInspector.AfterReceiveReply called.")
        Console.WriteLine("Message: {0}", reply.ToString())
    End Sub

    Public Function BeforeSendRequest(ByRef request As System.ServiceModel.Channels.Message, _
            ByVal channel As IClientChannel) As Object Implements IClientMessageInspector.BeforeSendRequest
        Console.WriteLine("IClientMessageInspector.BeforeSendRequest called.")
        Return Nothing
    End Function

다음 코드 예제에서는 클라이언트 엔드포인트에 System.ServiceModel.Description.IEndpointBehavior 클라이언트 메시지 검사기를 삽입 하는 방법을 보여 있습니다.

#region IEndpointBehavior Members
public void AddBindingParameters(
  ServiceEndpoint endpoint, BindingParameterCollection bindingParameters
) { return; }

public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
  clientRuntime.MessageInspectors.Add(new Inspector());
  foreach (ClientOperation op in clientRuntime.Operations)
    op.ParameterInspectors.Add(new Inspector());
}

public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
  endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new Inspector());
  foreach (DispatchOperation op in endpointDispatcher.DispatchRuntime.Operations)
    op.ParameterInspectors.Add(new Inspector());
}

public void Validate(ServiceEndpoint endpoint){ return; }
#Region "IEndpointBehavior Members"
    Public Sub AddBindingParameters(ByVal endpoint As ServiceEndpoint, ByVal bindingParameters _
                                    As BindingParameterCollection) Implements IEndpointBehavior.AddBindingParameters
        Return
    End Sub

    Public Sub ApplyClientBehavior(ByVal endpoint As ServiceEndpoint, ByVal clientRuntime As ClientRuntime) _
    Implements IEndpointBehavior.ApplyClientBehavior
        clientRuntime.MessageInspectors.Add(New Inspector())
        For Each op As ClientOperation In clientRuntime.Operations
            op.ParameterInspectors.Add(New Inspector())
        Next op
    End Sub

    Public Sub ApplyDispatchBehavior(ByVal endpoint As ServiceEndpoint, ByVal endpointDispatcher As  _
                                     EndpointDispatcher) Implements IEndpointBehavior.ApplyDispatchBehavior
        endpointDispatcher.DispatchRuntime.MessageInspectors.Add(New Inspector())
        For Each op As DispatchOperation In endpointDispatcher.DispatchRuntime.Operations
            op.ParameterInspectors.Add(New Inspector())
        Next op
    End Sub

Public Sub Validate(ByVal endpoint As ServiceEndpoint) Implements IEndpointBehavior.Validate
    Return
End Sub

마지막으로 다음 코드 예제에서는 특정 엔드포인트에서 엔드포인트 동작을 사용하도록 클라이언트 구성 파일을 수정하는 방법을 보여 있습니다.

  <client>
      <endpoint 
        address="http://localhost:8080/SampleService" 
        behaviorConfiguration="clientInspectorsAdded" 
        binding="wsHttpBinding"
        bindingConfiguration="WSHttpBinding_ISampleService" 
        contract="ISampleService"
        name="WSHttpBinding_ISampleService"
      >
      </endpoint>
  </client>
<behaviors>
  <endpointBehaviors>
    <behavior name="clientInspectorsAdded">
      <clientInterceptors />
    </behavior>
  </endpointBehaviors>
</behaviors>
<extensions>
  <behaviorExtensions>
    <add 
      name="clientInterceptors" 
      type="Microsoft.WCF.Documentation.InspectorInserter, HostApplication, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"
  />
  </behaviorExtensions>
</extensions>

설명

BeforeSendRequest 보내기 전에 요청 메시지를 검사하거나 수정하는 방법을 구현합니다.

적용 대상