共用方式為


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)

在作業傳回之後呼叫,但在傳送回復訊息之前呼叫。

適用於