compressioncastle-windsorwcffacility

Castle Windsor WCFFacility compression


In our current project we are using Castle Windsor to configure WCF on a .NET 4 Application. As the data we are moving back and forth may grow we would like to have some sort of compression (Espessially from client to server). Searching for this on google brought two solutions: Use the built in compression support of .net 45 (By now we are not able to move to .net 45 because we officially need to support Windows XP) or use the example encoder provided by microsoft (http://msdn.microsoft.com/en-us/library/ms751458.aspx). Since the custom encoder option doesn't look that bad, I would like to give it a try until we can move to .net 45. The only thing I really don't know is whether it is possible to configure this scenario using Castle Windsor.

Any ideas on this?

Thanks Markus


Solution

  • after some research of the source code of the involved WCF classes, we were able to find a pretty good solution for our needs: We are currently using the WSHttpBinding for our bindings. This has a method CreateBindingElements which returns a collection of the used binding elements and can be overridden. So we just derived from the WsHttpBinding class and injected the GZipMessageEncoding into the collection before returning it.

    public class GZipWSHttpBinding : WSHttpBinding
    {
        public override BindingElementCollection CreateBindingElements()
        {
            BindingElementCollection bec = base.CreateBindingElements();
    
            int index = bec.Count - 1;
            while (index >= 0 && !(bec[index] is MessageEncodingBindingElement))
                index--;
    
            if (index >= 0)
            {
                GZipMessageEncodingBindingElement gZipCompression =
                    new GZipMessageEncodingBindingElement(bec[index] as MessageEncodingBindingElement);
                bec[index] = gZipCompression;
            }
    
            return bec;
        }
    }
    

    The only thing left is using this class instead of the WsHttpBinding class in the Windsor installer, and gzip compression is in place.

    Best Regards Markus