IDispatchMessageInspector IDispatchMessageInspector IDispatchMessageInspector IDispatchMessageInspector Interface

定義

サービス アプリケーションで受信および送信アプリケーション メッセージのカスタム検査または変更を可能にするメソッドを定義します。Defines the methods that enable custom inspection or modification of inbound and outbound application messages in service applications.

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

次のコード例では、呼び出されるときにコンソールに文字列を書き出す基本的な IDispatchMessageInspector を示します。The following code example shows a basic IDispatchMessageInspector that writes a string to the console when it is invoked.

#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 の実装を示します。The following code example shows the implementation of an IServiceBehavior that adds the InspectorIDispatchMessageInspector to the DispatchRuntime.MessageInspectors collection.

#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 を挿入するサービス動作を読み込む方法を示しています。The following code example shows the use of an application configuration file to load the service behavior that inserts the 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 を実装します。Implement IDispatchMessageInspector to inspect or modify inbound or outbound application messages either prior to dispatching a request message to an operation or before returning a reply message to a caller. メッセージの宛先の操作を呼び出す前にメッセージをインターセプトする必要のあるシナリオは、数多くあります。There are a large number of scenarios that require intercepting messages prior to invoking the operation for which it is destined. たとえば、受信アプリケーション メッセージをログに記録したり、メッセージ ヘッダーに基づいてなんらかの機能を実行したりすることが考えられます。For example, you can log incoming application messages or perform some feature based on a message header.

通常、メッセージ インスペクターは、サービス動作 (System.ServiceModel.Description.IServiceBehavior)、エンドポイント動作 (System.ServiceModel.Description.IEndpointBehavior)、またはコントラクト動作 (System.ServiceModel.Description.IContractBehavior) により挿入されます。Typically, message inspectors are inserted by a service behavior (System.ServiceModel.Description.IServiceBehavior), an endpoint behavior (System.ServiceModel.Description.IEndpointBehavior), or a contract behavior (System.ServiceModel.Description.IContractBehavior). その後、動作は、メッセージ インスペクターを DispatchRuntime.MessageInspectors コレクションに追加します。The behavior then adds the message inspector to the DispatchRuntime.MessageInspectors collection. 動作を使用したランタイムの拡張の詳細については、「 ServiceHost とサービスモデルレイヤーの拡張」を参照してください。For more information about extending the runtime using behaviors, see Extending ServiceHost and the Service Model Layer.

  • AfterReceiveRequest メソッドを使用すると、メッセージを受信した後で、目的の操作にメッセージをディスパッチする前に、カスタム動作を実行できます。The AfterReceiveRequest method enables custom behavior after receiving the message but before dispatching it to the intended operation.

  • BeforeSendReply メソッドを使用すると、操作から返った後で、応答を送信する前に、カスタム動作を実行できます。The BeforeSendReply method enables custom behavior after the operation returns but before the reply is sent.

注意

操作が一方向か要求/応答かに関係なく、IDispatchMessageInspector オブジェクトは常に、メッセージ ディスパッチの間の同じ位置で呼び出されます。IDispatchMessageInspector objects are always called at the same point during message dispatch regardless of whether an operation is one-way or request-reply.

メソッド

AfterReceiveRequest(Message, IClientChannel, InstanceContext) AfterReceiveRequest(Message, IClientChannel, InstanceContext) AfterReceiveRequest(Message, IClientChannel, InstanceContext) AfterReceiveRequest(Message, IClientChannel, InstanceContext)

受信メッセージを受信した後で、ただし、目的の操作にメッセージをディスパッチする前に、呼び出されます。Called after an inbound message has been received but before the message is dispatched to the intended operation.

BeforeSendReply(Message, Object) BeforeSendReply(Message, Object) BeforeSendReply(Message, Object) BeforeSendReply(Message, Object)

操作から返った後で、ただし、応答メッセージを送信する前に、呼び出されます。Called after the operation has returned but before the reply message is sent.

適用対象