Channels 102b: Close vs. Abort (vs. Dispose)

A major source of confusion with Indigo Beta 1 is how to shut down our communication objects, so I’m going to drill further into this topic. First, I must note that this area is under review for Beta 2. We understand there are some inconsistencies between our usage of IDisposable and its usage by other parts of the .Net Framework. But the CTP bits are what you have to work with for now, so I will do my best to explain them 🙂

When I talked about the common state machine in Indigo, I neglected to point out that ICommunicationObject derives from IDisposable. This gives you a third option (in addition to our Close() and Abort() methods) for shutdown. It also allows you to use any ICommunicationObject with the C# using statement.

In the CTP bits, Dispose == Abort. Or put another way, Dispose != Close. This is a point worth re-iterating. Dispose will rudely abort your object.

So when you are done with an ICommunicationObject, what should you do?

First you should call Close(), which will flush any outstanding buffers, acknowledge outstanding data, and provide a graceful shutdown. This is similar to what happens when you call Stream.Close() or XmlWriter.Close(). Then you should make sure that Abort/Dispose is called if anything fails (i.e. throws an exception). This way any heavyweight network resources are released in an eager fashion.

Which leads to code like the following:

IOutputChannel channel = CreateOutputChannel();

using (channel)



2 thoughts on “Channels 102b: Close vs. Abort (vs. Dispose)

  1. Nicholas Paldino [.NET/C# MVP]

    This is a little ugly, IMO, as I would like to have the ability to leverage the using statement to close down gracefully.

    However, I understand why you can’t have it do both. In the end, I’ll end up writing a structure which will call close properly if the CommunicationState value is not closed.

    But, in order to do this, it would be nice if there was an Aborted value in the CommunicationState enumeration.

  2. Kenny

    Yes, I agree it’s a little ugly. We are considering adding an Aborted state in order to be explicit. Stay tuned.


Leave a Reply

Your email address will not be published. Required fields are marked *