Qrom
Qrom

Reputation: 497

Wcf : Where are all the responses going?

I have some REST web services implemented like this :

[ServiceContract]
public interface IRESTService
{
    [WebGet(UriTemplate = "GetEveryone", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    EveryoneDTO GetEveryone();
}

public class RESTService : IRESTService
{
    public EveryoneDTO GetEveryone()
    {
        // [...] Some processing
        return everyone;
    }
}

Where is going my everyone object? I mean, it must happens some things to transform the objects to JSON and send them. My debbugging won't lead me any further.

I'm interested in this because, let's say, I want to process every string contained in every objects I send back (maybe for encoding purposes), how/where would I be able to implement a middleware that intercept every object and be able to alter them easily before sending them?

Upvotes: 0

Views: 67

Answers (2)

Ricardo Pontual
Ricardo Pontual

Reputation: 3757

A good solution is to implement a Custom Message Inspector.
There are two interfaces you can implement, depending on client or server side:

IClientMessageInspector for client and IDispatchMessageInspector for server. You can implement both on same class and assembly and use what is more convenient, because message inspectors are extensions and you can configure (Web.config for instance) which you want to use.

IDispatchMessageInspector implements AfterReceiveRequest and BeforeSendReply methods, so you can intercept messages when you receive the request and before to send the reply, very usefull to your scenario.

Here is the MSDN message-inspectors documetation

A simple example of implementation:

    public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
    {
        MessageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue);
        request = buffer.CreateMessage();
        var m = buffer.CreateMessage().ToString();
        return null;
    }

Note that you must copy the original message, so you can extract the XML and convert to an object to your pourpose (log, change, etc)

Upvotes: 1

Mty
Mty

Reputation: 3

If i understood well, you would like to intercept WCF data output before sending it to your client.

there are various ways of doing that, what's in the link below is one of them :

https://www.codeguru.com/csharp/.net/net_wcf/learn-to-create-interceptors-in-wcf-services.htm

Hope it helps

Upvotes: 0

Related Questions