WCF – Custom Message Encoder and Operation Message Size

December 20, 2012

tags: ,
7 comments

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 –

  1. We need to have a way to get the incoming message size in our service implementation.
  2. 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:

Code Snippet
  1. [OperationMessageSizeBehavior]
  2. public class MyService : IMyService
  3. {
  4.     public void Do(Foo foo)
  5.     {
  6.         Console.WriteLine("MyService.Do() – Incoming request size: {0}KB",
  7.             OperationMessageSizeExtension.Current.SizeInBytes / 1024);
  8.     }
  9. }

As you can see the usage is very simple.
You define the behavior on your service, then you can access the extension to retrieve the size of the incoming request and do whatever you want with it.

One more tip – Keep the meta-data aligned:

I thought I should share a problem I experienced when I coded this – everything looked fine and it all worked.
However, when a developer tried to add a service reference against the service meta-data, he would get an incorrect binding.

I was wondering why it happened, my wrappers were delegating every single operation to the existing ones, so what was I missing?

Ahhhh.. Of course! Don’t forget about the ‘IWsdlExportExtension’ and ‘IPolicyExportExtension.
Encoding binding elements could be implementing those interfaces to participate in the building of the service meta-data.

I fixed the problem by implementing these interfaces myself and forward it to the existing binding element if it too has implemented it.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

7 comments

  1. レプリカ時計December 23, 2012 ב 6:00

    品質がよい、価格が低い、実物写真!当社の商品は絶対の自信が御座います。100%品質保証 !満足保障100%!

    Reply
  2. Fernando MôraJanuary 9, 2013 ב 20:49

    Hi Amir. Good article. Could you give me some explanation on how to get the response message size, before sent it to the client?

    Thanks.

    Reply
  3. Amir ZukerFebruary 21, 2013 ב 7:43

    Hi Fernando,

    Well, that depends on when you need it.
    As written in the post, the place where this is known for a fact is the message encoder, because that is the place where the message actually transforms to the bytes segments where you can measure its actual size.

    You could modify the code and add the ability to inject a behavior or action to be executed at this point and do whatever you need with this detail.

    Hope that helps,
    Amir Zuker

    Reply
  4. GraliaExhauraApril 21, 2013 ב 0:03

    It is tough to find knowledgeable men and women on this subject, but you sound like you know what you’re talking about! Thanks

    [url=http://www.topsmichaelkorsbags.com/]micheal kors[/url]
    [url=http://www.topsmichaelkorsbags.com/]nordstrom michael kors[/url]

    Reply
  5. GraliaExhauraMay 4, 2013 ב 6:35

    I was extremely pleased to come across this web-site.I wanted to thanks for your time for this amazing read!! I definitely enjoying every single small bit of it and I’ve you bookmarked to have a look at new stuff you blog post.

    [URL=http://www.mkhandbags4sale.com/handbags-c-52.html]kors by michael kors[/URL]

    Reply
  6. GraliaExhauraMay 8, 2013 ב 16:49

    Can I just say what a relief to uncover an individual who essentially knows what theyre talking about on the web. You surely know easy methods to bring an problem to light and make it very important. More many people have to read this and understand this side of the story. I cant believe youre not even more favorite due to the fact you undoubtedly have the gift.

    [URL=http://www.mkbagforcheap.com/totes-c-43.html]michael kors store[/URL]

    Reply
  7. GraliaExhauraMay 8, 2013 ב 23:27

    You can find some fascinating points in time in this post but I don’t know if I see all of them center to heart. There’s some validity but I will take hold opinion until I look into it further. Fine article , thanks and we want extra! Added to FeedBurner as well

    [URL=http://www.cheaps-michaelkors.com/wallets-c-46.html]michael kors website[/URL]

    Reply