Custom transport channels and BindingElements

When creating a custom transport channel, you must be sure to remove the encoding binding element from the binding context before returning from your BuildChannelFactory and BuildChannelListener calls. The best place to do this is within the constructor for your transport channel factory or listener.

 MessageEncodingBindingElement encodingBindingElement = context.BindingParameters.Remove<MessageEncodingBindingElement>();

Its needs to be removed because there is a validation step that is performed after the channel stack has been fully created. If there are binding elements left in the stack, an InvalidOperaionException will be thrown from Binding.BuildChannelFactory or Binding.BuildChannelListener.

Also, be aware if you are building a custom encoder; be sure to add your MessageEncodingBindingElement back to the BindingContext before returning from your BuildChannelListener/BuildChannelFactory methods of your MessageEncodingBindingElement back so that the transport is able to create your encoder. 

 context.BindingParameters.Add(this);