8

我有一个没有互操作性要求的自定义 Intranet 应用程序。我们以编程方式在双工模式下构造一个 NetTcp 通道来传递消息。我们想更改消息编码,但无法弄清楚如何实现。

我们采用的方法(未成功)是将 NetTcpBinding 扩展为一个名为 LeanTcpBinding 的新类,如下所示:


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;
    }
}

显然,我们正在尝试用我们自己的替换默认的 BinaryMessageEncodingBindingElement。只是为了开始使用,LeanBinaryMessageEncodingBindingElement 是 BinaryMessageEncodingBindingElement 的扩展,如下所示:


 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; }
        }
 }

当我尝试使用绑定时,它会完全按照我的想法去做......它取代了 BinaryMessageEncodingBindingElement。但是,我从来没有接到过任何对 LeanBinaryMessageEncodingBindingElement.CreateMessageEncoderFactory() 的调用,即使消息正在通过通道进行交换。

有人对如何正确执行此操作有任何建议吗?

4

2 回答 2

7

Kenny Wolf 澄清了遗漏的内容,并记录在下面的博客条目中。

http://kennyw.com/?p=170

对于不耐烦的人,问题在于默认情况下 MessageEncoderBindingElement 不会将自身添加到上下文的绑定参数中。结果,当传输稍后去查找 MessageEncoderBindingElement 时,它找不到我(或您)创建的那个,并且出现了我在原始帖子中提到的静默失败。

不幸的是,您必须按如下方式覆盖所有 CanBuildXXX 和 BuildXXX 方法,以确保将绑定元素添加到上下文的绑定参数中。


        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();
        }

于 2010-01-06T05:05:05.273 回答
2

您是否尝试过从您拥有的 NetTcpBinding 配置创建 CustomBinding?像这样的东西:

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);
于 2010-01-05T16:05:46.107 回答