共用方式為


IDispatchMessageInspector.AfterReceiveRequest 方法

定義

在收到輸入訊息之後呼叫,但在訊息分派至預定作業之前呼叫。

public:
 System::Object ^ AfterReceiveRequest(System::ServiceModel::Channels::Message ^ % request, System::ServiceModel::IClientChannel ^ channel, System::ServiceModel::InstanceContext ^ instanceContext);
public object AfterReceiveRequest (ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext);
abstract member AfterReceiveRequest : Message * System.ServiceModel.IClientChannel * System.ServiceModel.InstanceContext -> obj
Public Function AfterReceiveRequest (ByRef request As Message, channel As IClientChannel, instanceContext As InstanceContext) As Object

參數

request
Message

要求訊息。

channel
IClientChannel

傳入通道。

instanceContext
InstanceContext

目前的服務實例。

傳回

用來相互關聯狀態的物件。 這個物件會在 BeforeSendReply(Message, Object) 方法中傳回。

範例

下列程式代碼範例顯示基本 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>

備註

使用 AfterReceiveRequest 方法來檢查或修改傳入訊息、提供的用戶端通道,以及目前的服務實例。 傳回值可以是您想要用於相互關聯之任何物件;它會以 BeforeSendReply 方法中的 correlationState 參數傳回給您。

重要

除非您緩衝整個訊息並製作復本,否則您無法使用訊息本文。 如果您這樣做,您必須建立兩個訊息複本:一個要檢查,另一個則覆寫 request 參數。

適用於