Прочетете на английски Редактиране

Споделяне чрез


IDispatchMessageInspector Interface

Definition

Defines the methods that enable custom inspection or modification of inbound and outbound application messages in service applications.

C#
public interface IDispatchMessageInspector

Examples

The following code example shows a basic IDispatchMessageInspector that writes a string to the console when it is invoked.

C#
#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

The following code example shows the implementation of an IServiceBehavior that adds the Inspector IDispatchMessageInspector to the DispatchRuntime.MessageInspectors collection.

C#
#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());
    }
  }
}

The following code example shows the use of an application configuration file to load the service behavior that inserts the Inspector IDispatchMessageInspector.

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

Remarks

Implement IDispatchMessageInspector to inspect or modify inbound or outbound application messages either prior to dispatching a request message to an operation or before returning a reply message to a caller. There are a large number of scenarios that require intercepting messages prior to invoking the operation for which it is destined. For example, you can log incoming application messages or perform some feature based on a message header.

Typically, message inspectors are inserted by a service behavior (System.ServiceModel.Description.IServiceBehavior), an endpoint behavior (System.ServiceModel.Description.IEndpointBehavior), or a contract behavior (System.ServiceModel.Description.IContractBehavior). The behavior then adds the message inspector to the DispatchRuntime.MessageInspectors collection. For more information about extending the runtime using behaviors, see Extending ServiceHost and the Service Model Layer.

  • The AfterReceiveRequest method enables custom behavior after receiving the message but before dispatching it to the intended operation.

  • The BeforeSendReply method enables custom behavior after the operation returns but before the reply is sent.

Бележка

IDispatchMessageInspector objects are always called at the same point during message dispatch regardless of whether an operation is one-way or request-reply.

Methods

AfterReceiveRequest(Message, IClientChannel, InstanceContext)

Called after an inbound message has been received but before the message is dispatched to the intended operation.

BeforeSendReply(Message, Object)

Called after the operation has returned but before the reply message is sent.

Applies to

Продукт Версии
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)