다음을 통해 공유


방법: 서비스에서 메시지 검사 및 수정

System.ServiceModel.Dispatcher.IDispatchMessageInspector를 구현하고 서비스 런타임에 삽입하여 들어오거나 보내는 메시지를 WCF(Windows Communication Foundation) 클라이언트에서 검사하거나 수정할 수 있습니다. 자세한 내용은 디스패처 확장을 참조하십시오. 서비스의 해당 기능은 System.ServiceModel.Dispatcher.IClientMessageInspector입니다.

메시지를 검사하거나 수정하려면

  1. System.ServiceModel.Dispatcher.IDispatchMessageInspector 인터페이스를 구현합니다.

  2. 서비스 메시지 검사자를 쉽게 삽입하려는 범위에 따라 System.ServiceModel.Description.IServiceBehavior, System.ServiceModel.Description.IEndpointBehavior 또는 System.ServiceModel.Description.IContractBehavior 인터페이스를 구현합니다.

  3. System.ServiceModel.ServiceHost에서 System.ServiceModel.ICommunicationObject.Open 메서드를 호출하기 전에 동작을 삽입합니다. 자세한 내용은 동작을 사용하여 런타임 구성 및 확장을 참조하십시오.

예제

다음 코드 예제는 아래 순서대로 나열되어 있습니다.

  • 서비스 검사자 구현

  • 검사자를 삽입하는 서비스 동작

  • 서비스 응용 프로그램에서 동작을 로드 및 실행하는 구성 파일

#Region "IDispatchMessageInspector Members"
    Public Function AfterReceiveRequest(ByRef request As System.ServiceModel.Channels.Message, _
                       ByVal channel As IClientChannel, ByVal instanceContext As InstanceContext) _
                       As Object Implements IDispatchMessageInspector.AfterReceiveRequest
        Console.WriteLine("IDispatchMessageInspector.AfterReceiveRequest called.")
        Return Nothing
    End Function

    Public Sub BeforeSendReply(ByRef reply As System.ServiceModel.Channels.Message, ByVal correlationState As Object) _
    Implements IDispatchMessageInspector.BeforeSendReply
        Console.WriteLine("IDispatchMessageInspector.BeforeSendReply called.")
    End Sub
#End Region
#Region "IServiceBehavior Members"
    Public Sub AddBindingParameters(ByVal serviceDescription As ServiceDescription, _
                   ByVal serviceHostBase As ServiceHostBase, ByVal endpoints As  _
                   System.Collections.ObjectModel.Collection(Of ServiceEndpoint), _
                   ByVal bindingParameters As BindingParameterCollection) Implements IServiceBehavior.AddBindingParameters
        Return
    End Sub

    Public Sub ApplyDispatchBehavior(ByVal serviceDescription As ServiceDescription, _
                                     ByVal serviceHostBase As ServiceHostBase) Implements _
                                     IServiceBehavior.ApplyDispatchBehavior
        For Each chDisp As ChannelDispatcher In serviceHostBase.ChannelDispatchers
            For Each epDisp As EndpointDispatcher In chDisp.Endpoints
                epDisp.DispatchRuntime.MessageInspectors.Add(New Inspector())
                For Each op As DispatchOperation In epDisp.DispatchRuntime.Operations
                    op.ParameterInspectors.Add(New Inspector())
                Next op
            Next epDisp
        Next chDisp
    End Sub
<configuration>
  <system.serviceModel>
    <services>
      <service 
        name="Microsoft.WCF.Documentation.SampleService"
        behaviorConfiguration="inspectorBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="https://localhost:8080/SampleService" />
          </baseAddresses>
        </host>
        <endpoint
          address=""
          binding="wsHttpBinding"
          contract="Microsoft.WCF.Documentation.ISampleService"
        />
 
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="inspectorBehavior">
          <serviceInspectors />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <extensions>
      <behaviorExtensions>
        <add 
          name="serviceInspectors" 
          type="Microsoft.WCF.Documentation.InspectorInserter, HostApplication, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"
        />
      </behaviorExtensions>
    </extensions>
  </system.serviceModel>
</configuration>

참고 항목

참조

System.ServiceModel.Dispatcher.IClientMessageInspector
System.ServiceModel.Dispatcher.IDispatchMessageInspector

개념

동작을 사용하여 런타임 구성 및 확장