Bagikan melalui


IDispatchMessageInspector Antarmuka

Definisi

Menentukan metode yang memungkinkan inspeksi kustom atau modifikasi pesan aplikasi masuk dan keluar dalam aplikasi layanan.

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

Contoh

Contoh kode berikut menunjukkan IDispatchMessageInspector dasar yang menulis string ke konsol saat dipanggil.

#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

Contoh kode berikut menunjukkan implementasi IServiceBehavior yang menambahkan InspectorIDispatchMessageInspector ke koleksi 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

Contoh kode berikut menunjukkan penggunaan file konfigurasi aplikasi untuk memuat perilaku layanan yang menyisipkan 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>

Keterangan

Terapkan IDispatchMessageInspector untuk memeriksa atau memodifikasi pesan aplikasi masuk atau keluar baik sebelum mengirimkan pesan permintaan ke operasi atau sebelum mengembalikan pesan balasan ke pemanggil. Ada sejumlah besar skenario yang memerlukan penyadapan pesan sebelum memanggil operasi yang ditakdirkan. Misalnya, Anda dapat mencatat pesan aplikasi masuk atau melakukan beberapa fitur berdasarkan header pesan.

Biasanya, pemeriksa pesan disisipkan oleh perilaku layanan (System.ServiceModel.Description.IServiceBehavior), perilaku titik akhir (System.ServiceModel.Description.IEndpointBehavior), atau perilaku kontrak (System.ServiceModel.Description.IContractBehavior). Perilaku kemudian menambahkan pemeriksa pesan ke koleksi DispatchRuntime.MessageInspectors. Untuk informasi selengkapnya tentang memperluas runtime menggunakan perilaku, lihat Memperluas ServiceHost dan Service Model Layer.

  • Metode AfterReceiveRequest memungkinkan perilaku kustom setelah menerima pesan tetapi sebelum mengirimkannya ke operasi yang dimaksudkan.

  • Metode BeforeSendReply memungkinkan perilaku kustom setelah operasi kembali tetapi sebelum balasan dikirim.

Nota

IDispatchMessageInspector objek selalu dipanggil pada titik yang sama selama pengiriman pesan terlepas dari apakah operasi adalah satu arah atau balasan permintaan.

Metode

AfterReceiveRequest(Message, IClientChannel, InstanceContext)

Dipanggil setelah pesan masuk diterima tetapi sebelum pesan dikirim ke operasi yang dimaksudkan.

BeforeSendReply(Message, Object)

Dipanggil setelah operasi kembali tetapi sebelum pesan balasan dikirim.

Berlaku untuk