13

I am using the following ways to close the WCF 4 channels. Is this right way to do it?

using (IService channel 
    = CustomChannelFactory<IService>.CreateConfigurationChannel())
{
    channel.Open();

    //do stuff
}// channels disposes off??
tom redfern
  • 30,562
  • 14
  • 91
  • 126
chugh97
  • 9,602
  • 25
  • 89
  • 136

2 Answers2

26

That used to be the commonly accepted way to release WCF client proxies in the "early" days of WCF.

However things have since changed. It turned out that the implementation of IClientChannel<T>.Dispose() simply invokes the IClientChannel<T>.Close() method, which may throw an exception under some circumstances, such as when the underlying channel isn't open or can't be closed in a timely fashion.

Therefore it's not a good idea to invoke Close() within a catch block since that may leave behind some unreleased resources in case of an exception.

The new recommended way is to invoke IClientChannel<T>.Abort() within the catch block instead, in case Close() would fail. Here's an example:

try
{
    channel.DoSomething();
    channel.Close();
}
catch
{
    channel.Abort();
    throw;
}

Update:

Here's a reference to an MSDN article that describes this recommendation.

Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154
  • Do I really have to close the channel? I am thinking to leave it open and next time I use it I check if factory.State != CommunicationState.Opened (pipe factory in my case) and if true then recreating channel. – Valentin Jul 27 '12 at 08:42
  • 2
    @Valentin The short answer is **yes**, you *should* release a WCF channel *explicitly* when it is no longer needed. An open WCF channel holds some lower level resoures, such as a TCP socket or an HTTP connection. If you delay the release of these resources by leaving it in the *nondeterministic* hands of the Gargage Collector, you might run into a situation where they become scarce, which would make it impossible to create new channels. – Enrico Campidoglio Jul 29 '12 at 18:18
9

Although not strictly directed at the channel, you can do:

ChannelFactory<IMyService> channelFactory = null;
try
{
    channelFactory =
        new ChannelFactory<IMyService>();
    channelFactory.Open();

    // Do work...

    channelFactory.Close();
}
catch (CommunicationException)
{
    if (channelFactory != null)
    {
        channelFactory.Abort();
    }
}
catch (TimeoutException)
{
    if (channelFactory != null)
    {
        channelFactory.Abort();
    }
}
catch (Exception)
{
    if (channelFactory != null)
    {
        channelFactory.Abort();
    }
    throw;
}
tom redfern
  • 30,562
  • 14
  • 91
  • 126
  • 5
    This response is a bit off: it provides a usage pattern for the ChannelFactory, while the original question involves the channel. While it is true (I believe) that closing the factory will close all the channels that it created, a more common pattern is to keep a channelFactory open so that it can create multiple channels efficiently. Each channel should be individually closed while keeping the factory open for work. – Elroy Flynn Jun 06 '13 at 16:16
  • That is true - thank you for pointing that out. Have adjusted post to reflect – tom redfern Jun 10 '13 at 06:53