IDispatchMessageInspector インターフェイス

定義

サービス アプリケーションで受信および送信アプリケーション メッセージのカスタム検査または変更を可能にするメソッドを定義します。

public interface class IDispatchMessageInspector
public interface IDispatchMessageInspector
type IDispatchMessageInspector = interface
Public Interface IDispatchMessageInspector

次のコード例では、呼び出されるときにコンソールに文字列を書き出す基本的な IDispatchMessageInspector を示します。

#region IDispatchMessageInspector Members
public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel, InstanceContext instanceContext)
{
  Console.WriteLine("IDispatchMessageInspector.AfterReceiveRequest called.");
  return null;
}

public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
{
  Console.WriteLine("IDispatchMessageInspector.BeforeSendReply called.");
}
#endregion
#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

次のコード例では、IServiceBehaviorInspectorIDispatchMessageInspector に追加する DispatchRuntime.MessageInspectors の実装を示します。

#region IServiceBehavior Members
public void AddBindingParameters(
  ServiceDescription serviceDescription,
  ServiceHostBase serviceHostBase,
  System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints,
  BindingParameterCollection bindingParameters
)
{ return; }

public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
  foreach (ChannelDispatcher chDisp in serviceHostBase.ChannelDispatchers)
  {
    foreach (EndpointDispatcher epDisp in chDisp.Endpoints)
    {
      epDisp.DispatchRuntime.MessageInspectors.Add(new Inspector());
      foreach (DispatchOperation op in epDisp.DispatchRuntime.Operations)
        op.ParameterInspectors.Add(new Inspector());
    }
  }
}
#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

次のコード例では、アプリケーション構成ファイルを使用して、InspectorIDispatchMessageInspector を挿入するサービス動作を読み込む方法を示しています。

<configuration>
  <system.serviceModel>
    <services>
      <service 
        name="Microsoft.WCF.Documentation.SampleService"
        behaviorConfiguration="metadataSupport">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/SampleService" />
          </baseAddresses>
        </host>
        <endpoint
          address=""
          binding="wsHttpBinding"
          contract="Microsoft.WCF.Documentation.ISampleService"
        />
        <endpoint
           address="mex"
           binding="mexHttpBinding"
           contract="IMetadataExchange"
        />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="metadataSupport">
          <serviceMetadata httpGetEnabled="true" httpGetUrl=""/>
          <serviceInterceptors />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <extensions>
      <behaviorExtensions>
        <add 
          name="serviceInterceptors" 
          type="Microsoft.WCF.Documentation.InspectorInserter, HostApplication, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"
        />
      </behaviorExtensions>
    </extensions>
  </system.serviceModel>
</configuration>

注釈

要求メッセージを操作にディスパッチする前に、または応答メッセージを呼び出し元に返す前に、受信または送信アプリケーション メッセージを検査または変更するには、IDispatchMessageInspector を実装します。 メッセージの宛先の操作を呼び出す前にメッセージをインターセプトする必要のあるシナリオは、数多くあります。 たとえば、受信アプリケーション メッセージをログに記録したり、メッセージ ヘッダーに基づいてなんらかの機能を実行したりすることが考えられます。

通常、メッセージ インスペクターは、サービス動作 (System.ServiceModel.Description.IServiceBehavior)、エンドポイント動作 (System.ServiceModel.Description.IEndpointBehavior)、またはコントラクト動作 (System.ServiceModel.Description.IContractBehavior) により挿入されます。 その後、動作は、メッセージ インスペクターを DispatchRuntime.MessageInspectors コレクションに追加します。 動作を使用してランタイムを拡張する方法の詳細については、「 Extending ServiceHost and the Service Model Layer」を参照してください。

  • AfterReceiveRequest メソッドを使用すると、メッセージを受信した後で、目的の操作にメッセージをディスパッチする前に、カスタム動作を実行できます。

  • BeforeSendReply メソッドを使用すると、操作から返った後で、応答を送信する前に、カスタム動作を実行できます。

Note

操作が一方向か要求/応答かに関係なく、IDispatchMessageInspector オブジェクトは常に、メッセージ ディスパッチの間の同じ位置で呼び出されます。

メソッド

AfterReceiveRequest(Message, IClientChannel, InstanceContext)

受信メッセージを受信した後で、ただし、目的の操作にメッセージをディスパッチする前に、呼び出されます。

BeforeSendReply(Message, Object)

操作から返った後で、ただし、応答メッセージを送信する前に、呼び出されます。

適用対象