Home > WCF > Closing a WCF Client – The Proper Way!

Closing a WCF Client – The Proper Way!

I’ve finally gotten around to putting a bit of time into Windows Communication Foundation (WCF), more of which to follow.

There’s one particular ‘feature’ I came across which doesn’t seem too well advertised, which I though would be worth sharing, related to how you close the connection.

Now, with most things in .Net that implement IDisposable (database connections, network connections, and so on), you simply have to call Close() or Dispose() – or more commonly, just put them in a Using statement:

Closing a WCF client implicitly with a Using statement

The problem with this approach when it comes to WCF is that, unlike most things that you can Close(), when you call Close() on a WCF client, an exception might be thrown:

Exception during WCF client close with a Using statement

If you don’t know about the Using statement, it just calls Close() for you at the end of the code block so you don’t have to remember it.  You can see the same thing happening here using Close() explicitly:

Exception occurring on WCF client Close()

So, what’s happening here?  Well, unlike some of the other things you might call Close() on, the WCF client is doing some actual work in this method.  For a start, it’s connecting back to the service, which is something that could always fail due to network problems and so on – in the example above, I simply stopped the service before the client was closed.  There may also be transactions to be finalised, and many other things I don’t know about.

But do we really need to care?  Well, it’s hopefully not something that will happen a lot, but knowing the systems I’ve worked on, I’m sure it will happen at some point.  And the chances are, you don’t really care about this exception – you’ve finished using the service anyway.  So although it may be something you want to log, it probably isn’t a good enough reason to stop your app running, or disturb the user with a random message.  It also means that your connection hasn’t been closed down properly, which could cause problems.

‘So what’s the fix already?!’, I hear you cry.  Alright, I’m getting there!  In the basic case, it’s as simple as putting a try-catch around the call to Close(), and calling Abort() on the client if something bad happens (which will make sure everything’s cleaned up properly):

A better way to close a WCF client

And if you want to go the full hog and get one more tick in your ‘best practices’ box, you can follow this pattern (as pinched from Steve Smith’s blog):

The Proper way to close a WCF client

Obviously, you can add logging in as appropriate.

This is admittedly a bit of a hassle, but you can always put it into a helper class or, as Steve suggests in the above blog, write an extension method on ICommunicationObject which will then work for all of your clients.  I haven’t tried this, but it seems like a good approach.

So there you have it – one less thing to worry about with WCF!  (Or is that one more thing to worry about..)

Categories: WCF Tags:
  1. Stian
    October 31, 2012 at 11:02 am

    Cheers, but you haven’t handled the FaultedStateException, needs to be aborted as the other ones there.

    • Ling
      February 20, 2013 at 9:56 pm

      CommunicationException is a superclass that includes CommunicationObjectFaultedException

  2. Tom
    November 22, 2012 at 10:27 am


    I was wondering what could possibly go wrong if instead of the three types of exceptions you mentioned in your article you would cater for System.Exception only?
    Could you advise please?
    Thanks in advance.

    • relentless development
      February 21, 2013 at 9:54 am

      Hi Tom, sorry for the late response. There wouldn’t be a problem only catering for System.Exception when closing the connection, just that you might want to handle other exceptions differently. (For example, in the code above, System.Exception gets re-thrown but the others just abort the connection.)

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: