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?