This documentation is archived and is not being maintained.

IDispatchMessageInspector.AfterReceiveRequest Method

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

Namespace:  System.ServiceModel.Dispatcher
Assembly:  System.ServiceModel (in System.ServiceModel.dll)

Object AfterReceiveRequest(
	ref Message request,
	IClientChannel channel,
	InstanceContext instanceContext


Type: System.ServiceModel.Channels.Message%

The request message.

Type: System.ServiceModel.IClientChannel

The incoming channel.

Type: System.ServiceModel.InstanceContext

The current service instance.

Return Value

Type: System.Object
The object used to correlate state. This object is passed back in the BeforeSendReply method.

Use the AfterReceiveRequest method to inspect or modify the incoming message, the client channel that provided it, and the current service instance. The return value can be any object that you want to use for correlation purposes; it is returned to you as the correlationState parameter in the BeforeSendReply method.

Important noteImportant Note:

You cannot use the body of the message unless you buffer the entire message and make a copy. If you do this, you must make two copies of the message: one to inspect, and the other to overwrite the request parameter.

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

#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.");

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

#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 InspectorIDispatchMessageInspector.

            <add baseAddress="http://localhost:8080/SampleService" />

        <behavior name="inspectorBehavior">
          <serviceInspectors />
          type="Microsoft.WCF.Documentation.InspectorInserter, HostApplication, Version=, Culture=neutral, PublicKeyToken=null"

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0