0

Can we debug the request data and response data exchanged between WCF and the web client? If this is possible, please let me know how. My actual requirement is I wanted to be able to manipulate (apply a regex/remove null characters) the response that is sent by WCF.

Please advise.

koopaking3
  • 3,375
  • 2
  • 25
  • 36
Shikhar
  • 43
  • 1
  • 8
  • Take a [look at](http://stackoverflow.com/questions/4271517/how-to-turn-on-wcf-tracing) – StuartLC May 17 '14 at 19:24
  • @StuarLC - WCF Tracing will not allow the OP to manipulate the response sent by the service. – Tim May 18 '14 at 00:56
  • @Tim - It is not clear if he wants to do that in debugging, or if he just wants to use debugging to see if his manipulations are working. – Erik Funkenbusch May 18 '14 at 01:03

2 Answers2

1

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.

koopaking3
  • 3,375
  • 2
  • 25
  • 36
0

In order to intercept and possibly adapt parameters and return objects of the WCF invocations you can use your custom implementations of the IOperationsInvoker and IOperationBehavior. For an example of that and further extensibility possibilities in WCF take a look at the following article.

One additional notion about your question: The title and text do not really match. In the title you write Debugging whereas in the text you mention adapting of the return objects which is more than a sole debugging. If you just want to debug the parameters and return objects, i.e. have a look at them, you might find turning on WCF trace more appropriate.

Edin
  • 1,476
  • 11
  • 21