I've used IClientMessageInspector
for this very purpose with great success. It will allow you to view the request/reply and edit them before they continue on through the WCF client. The MSDN documentation is fairly clear on how to use it but here are the basic parts (in C#):
1) A class that implements IClientMessageInspector
. This is where your viewing and editing takes place using the reply
or request
objects passed to you:
public class MyMessageInspector : IClientMessageInspector
{
public void AfterReceiveReply(
ref Message reply,
object correlationState)
{
Console.WriteLine(
"Received the following reply: '{0}'", reply.ToString());
}
public object BeforeSendRequest(
ref Message request,
IClientChannel channel)
{
Console.WriteLine(
"Sending the following request: '{0}'", request.ToString());
return null;
}
}
2) A class that implements IEndpointBehavior
where you add the MyMessageInspector
to an endpoint behavior:
public class MyBehavior : IEndpointBehavior
{
public void AddBindingParameters(
ServiceEndpoint endpoint,
BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(
ServiceEndpoint endpoint,
ClientRuntime clientRuntime)
{
clientRuntime.MessageInspectors.Add(new MyMessageInspector());
}
public void ApplyDispatchBehavior(
ServiceEndpoint endpoint,
EndpointDispatcher endpointDispatcher)
{
}
public void Validate(
ServiceEndpoint endpoint)
{
}
}
3) And finally, add MyBehavior
to your endpoint like this (assuming you already have your client and config file already configured):
client.Endpoint.Behaviors.Add(new MyBehavior());
This will capture all requests/replies going through the given client endpoint.