Disposing a WCF Proxy

A common misconception is to think that a WCF proxy can be used and disposed as any other regular class that implements IDisposable. However, the IDisposable implementation in the WCF client channel is not like any other, and it can throws exceptions. A phrase I got from this thread in the WCF forum will give you a better context about this scenario,

“I think a key thing to notice is that Close() often implies doing "real work" that may fail, including network communication handshakes to shutdown sessions, committing transactions, etc.”

As consequence of this, we should add some code to handle any possible exception for network errors when a proxy is closed/disposed.

The common pattern for cleaning up a proxy is the following,

try
{
...
client.Close();
}
catch (CommunicationException e)
{
...
client.Abort();
}
catch (TimeoutException e)
{
...
client.Abort();
}
catch (Exception e)
{
...
client.Abort();
throw;
}

The problem is, I do not want to have that code everywhere in my application. A good solution for that is to use a helper class like this one.

Michelle recently announced a new project that automatically generates a proxy helper class like that in Visual Studio. http://wcfproxygenerator.codeplex.com/

That is a feature I would like to see out of the box in the next version of WCF :).

Published Friday, June 26, 2009 9:39 AM by cibrax
Filed under: ,

Comments

No Comments