Pablo M. Cibraro (aka Cibrax)

My thoughts on Web Services and .NET development

News

Pablo Cibraro's VisualCV

Blogs

Personal

Programming

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 :).

Posted: Jun 26 2009, 09:39 AM by cibrax | with 2 comment(s)
Filed under: ,

Comments

Sam Gentile's Blog (if (DeveloperTask == Communication && OS == Windows) said:

SOA/WCF/REST/WF Jesus Rodriguez has his slide deck on WOA from SOAWorld 2009 available Also, he has Using WCF 4.0 XAML Services in the real world: Activating WCF Services from a Central Repository SOA Security Testing - XML Gateways WCF Load Test Tool

# June 26, 2009 6:14 PM

links for 2009-06-29 « dstelow notes… said:

Pingback from  links for 2009-06-29 «  dstelow notes…

# June 29, 2009 7:05 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)