Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you programmatically change the WCF message encoding for NetTcp?

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?

like image 867
Ajaxx Avatar asked Jan 05 '10 13:01

Ajaxx


1 Answers

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

http://kennyw.com/?p=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();
        }

like image 135
Ajaxx Avatar answered Nov 05 '22 23:11

Ajaxx