The Ties that Bind Us, Part 1: BindingElement

On the road to building a custom channel, we're going to need to make use of a lot of small but very important pieces of the WCF object model.  The piece that I will be talking about for a few days this week is the B in the ABC's: bindings.  Bindings are a little mysterious because how to make use of them seems to change quite a bit from one preview release to another.  I think that bindings are getting very close to the end of this long journey though and will remain pretty much unchanged to Version 1 and forever beyond.

The first part of bindings that I'll show off is the binding element.  A binding element corresponds to a configurable entity that is part of the channel stack.

 public abstract class BindingElement
{
   protected BindingElement();
   protected BindingElement(BindingElement elementToBeCloned);

   public virtual IChannelFactory<TChannel> BuildChannelFactory<TChannel>(BindingContext context);
   public virtual bool CanBuildChannelFactory<TChannel>(BindingContext context);
   public abstract BindingElement Clone();
   public abstract T GetProperty<T>(BindingContext context) where T : class;
}

There are only a few methods that make up a binding element so I can go over them individually.  Two methods that get a lot of use are CanBuildChannelFactory and BuildChannelFactory.  Channel factories are a subject for the not-too-distant future, but in short these methods are responsible for validating that the configuration we have is going to lead to a valid channel stack and actually building that channel stack.  These methods are going to appear in other classes as well to do the work of tying together channels.

Binding elements are somewhat interchangeable pieces and to promote that mix-and-match behavior without causing too many headaches, we require that every binding element support duplicating itself.  That appears as a Clone method on the element and a copy constructor for implementing cloning.  Then, there are two ways of configuring how the binding element behaves.  The author of the binding element class is going to add some properties or methods that let you directly manipulate settings.  We also have a GetProperty method that allows configuring the binding element by having it look at other resources that have been placed into the binding.

The binding context is actually not that important for the sample channel that I'll be building in future posts, so I will hold off on describing contexts until later.  I will also leave the in-depth description of the GetProperty method until talking about some of the channels we have that actually make use of this lookup mechanism.

One kind of binding element that we do need in the channel sample is a binding element that describes how to encode messages.  That's where we'll pick up for tomorrow's post.

Next time: Encoding Messages