How to: Inspect and Modify Messages on the Service
You can inspect or modify the incoming or outgoing messages across a Windows Communication Foundation (WCF) client by implementing a System.ServiceModel.Dispatcher.IDispatchMessageInspector and inserting it into the service runtime. For more information, see Extending Dispatchers. The equivalent feature on the service is the System.ServiceModel.Dispatcher.IClientMessageInspector.
To inspect or modify messages
Implement the System.ServiceModel.Dispatcher.IDispatchMessageInspector interface.
Implement a System.ServiceModel.Description.IServiceBehavior, System.ServiceModel.Description.IEndpointBehavior, or System.ServiceModel.Description.IContractBehavior interface depending upon the scope at which you want to easily insert your service message inspector.
Insert your behavior prior to calling the System.ServiceModel.ICommunicationObject.Open method on the System.ServiceModel.ServiceHost. For details, see Configuring and Extending the Runtime with Behaviors.
Example
The following code examples show, in order:
A service inspector implementation.
A service behavior that inserts the inspector.
A configuration file that loads and runs the behavior in a service application.
#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 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());
}
}
}
<configuration>
<system.serviceModel>
<services>
<service
name="Microsoft.WCF.Documentation.SampleService"
behaviorConfiguration="inspectorBehavior">
<host>
<baseAddresses>
<add baseAddress="https://localhost:8080/SampleService" />
</baseAddresses>
</host>
<endpoint
address=""
binding="wsHttpBinding"
contract="Microsoft.WCF.Documentation.ISampleService"
/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="inspectorBehavior">
<serviceInspectors />
</behavior>
</serviceBehaviors>
</behaviors>
<extensions>
<behaviorExtensions>
<add
name="serviceInspectors"
type="Microsoft.WCF.Documentation.InspectorInserter, HostApplication, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"
/>
</behaviorExtensions>
</extensions>
</system.serviceModel>
</configuration>
See Also
Reference
System.ServiceModel.Dispatcher.IClientMessageInspector
System.ServiceModel.Dispatcher.IDispatchMessageInspector
Concepts
Configuring and Extending the Runtime with Behaviors
© 2007 Microsoft Corporation. All rights reserved.
Build Date: 2009-08-07