我有一个没有互操作性要求的自定义 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() 的调用,即使消息正在通过通道进行交换。
有人对如何正确执行此操作有任何建议吗?