views:

389

answers:

2

I have a custom intranet application that has no interoperability requirements. We programatically construct a NetTcp channel in duplex mode for passing messages. We wanted to change the message encoding but haven't been able to figure out how to make that happen.

The approach we have taken (unsuccessfully) has been to extend the NetTcpBinding into a new class called LeanTcpBinding as follows:


internal class LeanTcpBinding : NetTcpBinding
{
    private static readonly ILog _log =
        LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    public override BindingElementCollection CreateBindingElements()
    {
        BindingElementCollection bindingElementCollection = base.CreateBindingElements();
        BindingElement encodingElement = bindingElementCollection.FirstOrDefault(
            bindingElement => bindingElement is BinaryMessageEncodingBindingElement);
        if (encodingElement != null)
        {
            int index = bindingElementCollection.IndexOf(encodingElement);
            bindingElementCollection.RemoveAt(index);
            bindingElementCollection.Insert(index, new LeanBinaryMessageEncodingBindingElement());
        }
        else
        {
            _log.Warn("Encoding not found");
        }

        return bindingElementCollection;
    }
}

Obviously, we're trying to replace the default BinaryMessageEncodingBindingElement with our own. Just to get use started, the LeanBinaryMessageEncodingBindingElement is an extension of the BinaryMessageEncodingBindingElement as follows:


 internal class LeanBinaryMessageEncodingBindingElement : MessageEncodingBindingElement
 {
        private readonly BinaryMessageEncodingBindingElement _bindingElement;

        /// 
        /// Initializes a new instance of the  class.
        /// 
        public LeanBinaryMessageEncodingBindingElement()
        {
            _bindingElement = new BinaryMessageEncodingBindingElement();
        }

        /// 
        /// Initializes a new instance of the  class.
        /// 
        /// The binding element.
        public LeanBinaryMessageEncodingBindingElement(BinaryMessageEncodingBindingElement bindingElement)
        {
            _bindingElement = bindingElement;
        }

        /// 
        /// Initializes a new instance of the  class.
        /// 
        /// The element to be cloned.
        /// The binding element.
        public LeanBinaryMessageEncodingBindingElement(MessageEncodingBindingElement elementToBeCloned, BinaryMessageEncodingBindingElement bindingElement)
            : base(elementToBeCloned)
        {
            _bindingElement = bindingElement;
        }

        /// 
        /// When overridden in a derived class, returns a copy of the binding element object.
        /// 
        /// 
        /// A  object that is a deep clone of the original.
        /// 
        public override BindingElement Clone()
        {
            return new LeanBinaryMessageEncodingBindingElement(
                (BinaryMessageEncodingBindingElement)_bindingElement.Clone());
        }

        /// 
        /// When overridden in a derived class, creates a factory for producing message encoders.
        /// 
        /// 
        /// The  used to produce message encoders.
        /// 
        public override MessageEncoderFactory CreateMessageEncoderFactory()
        {
            return new LeanBinaryMessageEncoderFactory(_bindingElement.CreateMessageEncoderFactory());
        }

        /// 
        /// When overridden in a derived class, gets or sets the message version that can be handled by the message encoders produced by the message encoder factory.
        /// 
        /// 
        /// The  used by the encoders produced by the message encoder factory.
        /// 
        public override MessageVersion MessageVersion
        {
            get { return _bindingElement.MessageVersion; }
            set { _bindingElement.MessageVersion = value; }
        }
 }

When I try to use the binding it does exactly what I think it should do ... it replaces the BinaryMessageEncodingBindingElement. However, I never get any calls to the LeanBinaryMessageEncodingBindingElement.CreateMessageEncoderFactory(), even though messages are being exchanged across the channel.

Anyone have any suggestions on how to do this properly?

A: 

Have you tried creating a CustomBinding instead from the NetTcpBinding configuration you have? Something like this:

NetTcpBinding binding = new NetTcpBinding(); // get binding from somewhere
var elements = binding.CreateBindingElements();
BindingElementCollection newElements = new BindingElementCollection();
foreach ( var e in elements ) {
   if ( e is MessageEncodingBindingElement ) {
      newElements.Add(new MyMessageEncodingBindingElement());
   } else {
      newElements.Add(e);
   }
}
CustomBinding cb = new CustomBinding(newElements);
tomasr
I tried that method as well. The CreateMessageEncoderFactory() method is still not being called on the LeanBinaryMessageEncodingBindingElement - result is, my encoder isn't being constructed at all. I have to be missing something obvious.
Ajaxx
A: 

Kenny Wolf clarified what was missing and it's documented in the blog entry below.

http://kennyw.com/indigo/170

For the impatient, the problem is that by default the MessageEncoderBindingElement does not add itself to the context's binding parameters. As a result, when the transport later goes to find the MessageEncoderBindingElement it can not find the one that I (or you) have created and has the silent failure that I noted in my original post.

Unfortunately, you'll have to override all of the CanBuildXXX and BuildXXX methods as follows to ensure that you are adding the binding element to the binding parameters of the context.


        public override bool CanBuildChannelFactory(BindingContext context)
        {
            if (context == null) {
                throw new ArgumentNullException("context");
            }

            context.BindingParameters.Add(this);
            return context.CanBuildInnerChannelFactory();
        }

        public override bool CanBuildChannelListener(BindingContext context)
        {
            if (context == null) {
                throw new ArgumentNullException("context");
            }

            context.BindingParameters.Add(this);
            return context.CanBuildInnerChannelListener();
        }

        public override IChannelFactory BuildChannelFactory(BindingContext context)
        {
            if (context == null) {
                throw new ArgumentNullException("context");
            }

            context.BindingParameters.Add(this);
            return context.BuildInnerChannelFactory();
        }

        public override IChannelListener BuildChannelListener(BindingContext context)
        {
            if (context == null) {
                throw new ArgumentNullException("context");
            }

            context.BindingParameters.Add(this);
            return context.BuildInnerChannelListener();
        }

Ajaxx