次の方法で共有


方法 : サービスのメッセージを検査および変更する

System.ServiceModel.Dispatcher.IDispatchMessageInspector を実装し、それをサービス ランタイムに挿入することで、Windows Communication Foundation (WCF) クライアントの送受信メッセージを検査または変更できます。詳細については、「ディスパッチャーの拡張」を参照してください。サービスの同等の機能は、System.ServiceModel.Dispatcher.IClientMessageInspector です。

メッセージを検査または変更するには

  1. System.ServiceModel.Dispatcher.IDispatchMessageInspector インターフェイスを実装します。

  2. サービス メッセージ インスペクターを容易に挿入したいスコープに応じて、System.ServiceModel.Description.IServiceBehaviorSystem.ServiceModel.Description.IEndpointBehavior、または System.ServiceModel.Description.IContractBehavior インターフェイスを実装します。

  3. System.ServiceModel.ServiceHostSystem.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

概念

動作を使用したランタイムの構成と拡張