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

下列程式碼範例顯示 將 加入 Inspector IDispatchMessageInspector 至集合的 IServiceBehavior 實作 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

下列程式碼範例示範如何使用應用程式組態檔載入插入 Inspector IDispatchMessageInspector 的服務行為。

<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)

會在作業回傳之後但傳送回覆訊息之前初始化呼叫。

適用於