次の方法で共有


Using the Base Classes to Build Channels

After a bit of a diversion, let's spend some time again looking at the parts necessary to build a custom channel.  I hope to use the second half of this month going over a custom transport I've put together as a quick sample.  We've seen most of the important interfaces in the channel model by this point.  Today and tomorrow I'll go over base classes that make implementing those interfaces easier.  Today's post is about the ChannelBase class, which is the abstract base class for IChannel.

 public abstract class ChannelBase : CommunicationObject, IChannel, ICommunicationObject, IDisposable, IDefaultCommunicationTimeouts
{
   protected ChannelBase(ChannelManagerBase channelManager);

   protected override TimeSpan DefaultCloseTimeout { get; }
   protected override TimeSpan DefaultOpenTimeout { get; }
   protected TimeSpan DefaultReceiveTimeout { get; }
   protected TimeSpan DefaultSendTimeout { get; }
   public IChannelManager Manager { get; }

   public virtual T GetProperty<T>() where T : class;
   protected override IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state);
   protected override void OnClosed();
   protected override void OnEndOpen(IAsyncResult result);
   protected override void OnOpen(TimeSpan timeout);
}

ChannelBase can only be used in conjunction with the base classes for channel factories and listeners.  Right now, it's an all-or-nothing package.  There are a couple of things you get if you accept the package.  One is that the timeouts on the manager get used as the default timeouts for the channel.  Another is that ChannelBase takes care of managing the manager for you.  In addition to exposing a reference to the manager, the base class also notifies the manager when channels are created and destroyed.  The reference to the manager is only good until the channel is destroyed, so be mindful of the state your channel is in.

Even if you do use the base class, you'll still need to provide some sensible behavior for opening and closing the channel.  The default implementations of these methods are not going to do anything useful for you.  You'll also want to get into the habit of overriding GetProperty whenever you implement a channel.  GetProperty is an excellent way to communicate information along the channel stack without having to know what channels are below you, or what channels above you are interested in performing queries.  Define interfaces for people to query on for any interesting information you have.  Whenever you don't recognize the type argument for GetProperty, delegate down to the next channel in the stack or return null if you're creating a new transport channel at the bottom of the stack.

Next time: Using the Base Classes to Build Things that Build Channels

Comments

  • Anonymous
    April 05, 2006
    In yesterday's post, we looked at the first three subtle rules, aka pitfalls, of the channel model.&amp;nbsp;...
  • Anonymous
    April 07, 2006
    After seeing the ChannelBase class yesterday for implementing a channel, today's post is about the base...
  • Anonymous
    April 18, 2006
    During the last few weeks I've been going over various parts of the channel model and giving some commentary...
  • Anonymous
    May 24, 2006
    Today's post is a light entr&#233;e covering the IDefaultCommunicationTimeouts interface.  This interface...
  • Anonymous
    October 24, 2006
    In yesterday's post, we looked at the first three subtle rules , aka pitfalls, of the channel model.