This is a mirror of official site: http://jasper-net.blogspot.com/

WCF – Custom Message Encoder and Operation Message Size

| Monday, December 24, 2012
You may find the need to determine the request and reply message size when calling a service operation. 
It can be for logging purposes, or a practical one such as deciding whether you need to compress the data or not.

The key component in WCF that knows about the actual size of the message is the message encoder, and of course, you can plug-in your own encoder as part of WCF’s extensibility point. Ahhh, got to love this framework!

Lets examine the feature requirements first -

We need to have a way to get the incoming message size in our service implementation.
We need a way to enable the client to get the reply message size.
You can download the code here.

Basically, in order to implement a message encoder and to plug it in, you need to implement your own MessageEncoder, MessageEncoderFactory, and MessageEncodingBindingElement. 
Additionally, since this is part of the binding elements, we need to plug it in as part of the binding.

The implementation is fairly simple, since all I did was wrap the existing ones and added my message headers in the proper place. 
I will not show this code here, it’s pretty straight forward, I recommend you just download the code and take a look at it.

Finally, I implemented a behavior that you can attach to your service which does all the heavy lifting for you. 
It actually switches the current binding with a wrapper binding that plugs in our custom message encoder, pretty sweet.

In conclusion, here’s how our service would look like:

[OperationMessageSizeBehavior]
public class MyService : IMyService
{
    public void Do(Foo foo)
    {
        Console.WriteLine("MyService.Do() - Incoming request size: {0}KB",
            OperationMessageSizeExtension.Current.SizeInBytes / 1024);
    }
}

QR: Inline image 1

Posted via email from Jasper-net

0 comments: