Defines the methods that enable custom inspection or modification of inbound and outbound application messages in service applications.
Assembly: System.ServiceModel (in System.ServiceModel.dll)
|AfterReceiveRequest(Message, IClientChannel, InstanceContext)|
Called after an inbound message has been received but before the message is dispatched to the intended operation.
Called after the operation has returned but before the reply message is sent.
Implementto 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.
objects are always called at the same point during message dispatch regardless of whether an operation is one-way or request-reply.
The following code example shows a basicthat 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
#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
The following code example shows the use of an application configuration file to load the service behavior that inserts the Inspector.
Available since 3.0