One question I get from custom channel authors has to do with the various lifetimes of the components involved. Especially since, as per best practice, their heavyweight resources are attached to ChannelFactories and ChannelListeners and simply referenced from the Channel. Nicholas covered the basics in this post. Which to summarize is:
- ChannelFactory created Channels cannot outlive their creator
- channelFactory.Close/Abort will Close/Abort all Channels created by that factory
- channelListener.Close/Abort simply disables the ability to accept new channels; existing channels can continue to be serviced
Which makes life on the ChannelFactory end pretty straightforward. On the ChannelListener side, there are a few subtleties.
First, a Channel is owned by the listener from the time of creation until the successful completion of channel.Open. So if you are writing a custom listener that has offered up channels, you should clean them up if and only if they haven’t been opened.
Second, in order to perform eager disposal, you will need to track active ownership of your heavyweight resources. If there are opened channels, then you need to make sure that the shared resources that they leverage have their ownership transferred from the listener to your active channel(s). This can be accomplished through ref-counting, active transfers, or other mechanisms.