Share via


...Can Also Come Out (IOutputChannel)

Yesterday, we got a look at the reader side of the one-way communication pattern, which is implemented using IInputChannel.  Today, we're looking at the writer side of that pattern.

IOutputChannel bears a striking resemblance to IInputChannel but with only one of the three varieties of methods that we saw previously.  I won't repeat all of the references again, so go back to yesterday's post if you need a copy.

 public interface IOutputChannel : IChannel, ICommunicationObject, IDisposable
{
   EndpointAddress RemoteAddress { get; }
   Uri Via { get; }

   IAsyncResult BeginSend(Message message, AsyncCallback callback, object state);
   IAsyncResult BeginSend(Message message, TimeSpan timeout, AsyncCallback callback, object state);
   void EndSend(IAsyncResult result);
   void Send(Message message);
   void Send(Message message, TimeSpan timeout);
}

We just have the imperative variety of Send because it doesn't really make sense to TryToSend a message or to WaitToSend a message.  That's a little asymmetry between sending and receiving, but there's no reason to put those extra methods in if they don't do anything useful.  We do offer both synchronous and asynchronous flavors of Send, as well as explicit timeout and default timeout flavors.  I say this a lot, but I'll say it again: use the version with explicit timeouts.

One other difference that you might have noticed is that in addition to the RemoteAddress, which is where messages sent through this channel will end up, is that there is a Via property.  The via is the first hop that a message should take on its way to the remote address.  In most standard Internet scenarios, the via is the same as the final address.  You only need to change this when you're doing some kind of manual routing.  I'll have more to say about endpoints and vias in the future.

And remember, as a totally free bonus, by combining an IInputChannel and IOutputChannel, we've produced an IDuplexChannel that implements one of our two-way communication patterns.  Here's that diagram again.

It really is as simple as the picture shows.

Next time: It Makes the WWW Go Round, Part 1: IRequestChannel