But only if nothing goes wrong.
If the service you’re calling throws an exception (or Faults) then the Clients dispose method will throw another exception – masking the actual problem… There’s an entire MSDN article on the issue. The suggested fix? Replace the using block above with something like this:
WCF not looking so sweet now, is it?
Now, I’ve got an issue with this type of boilerplate code, it’s verbose and awkward and reeks of having to clean up someone else's mess. So, what can we do about? Well, there’s a couple of good ideas out there from replacing the default ClientBase to adding an additional partial class to the generated code. These are good solutions, but will require modification of the generated code and I’m too lazy to do that for each service I’m consuming.
So, I want a way to leverage the existing, generated client and keep the implementation as close to the neat using statement as possible. Thankfully, through the wonders of Generics and the Adapter pattern WCF can be redeemed – here’s TidyProxy:So, what does this get me? Well, to call my magically complex service I now just need to write: