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

下面的代码示例演示将 InspectorIDispatchMessageInspector 添加到 DispatchRuntime.MessageInspectors 集合的 IServiceBehavior 的实现。

#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 集合。 有关使用行为扩展运行时的详细信息,请参阅 扩展 ServiceHost 和服务模型层

  • AfterReceiveRequest 方法在收到消息后启用自定义行为,但在将其调度到预期操作之前。

  • BeforeSendReply 方法在操作返回后但在发送答复之前启用自定义行为。

注意

无论操作是单向还是请求-答复,IDispatchMessageInspector 对象始终在消息调度期间在同一点调用。

方法

AfterReceiveRequest(Message, IClientChannel, InstanceContext)

在收到入站消息之后,但在将消息调度到预期操作之前调用。

BeforeSendReply(Message, Object)

在操作返回后调用,但在发送回复消息之前调用。

适用于