IDispatchMessageInspector.AfterReceiveRequest Method (Message, IClientChannel, InstanceContext)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

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)

Function AfterReceiveRequest (
	ByRef request As Message,
	channel As IClientChannel,
	instanceContext As InstanceContext
) As Object


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.


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

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

#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
       End Sub

       Public Sub ApplyDispatchBehavior(ByVal serviceDescription As ServiceDescription, _
                                        ByVal serviceHostBase As ServiceHostBase) Implements _
           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

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

.NET Framework
Available since 3.0
Return to top