title: What is the best workaround for the WCF client `using` block issue?
body: <p>I like instantiating my WCF service clients within a <code>using</code> block as it's pretty much the standard way to use resources that implement <code>IDisposable</code>:</p>
  <pre><code>using (var client = new SomeWCFServiceClient()) 
  {
      //Do something with the client 
  }
  </code></pre>
  <p>But, as noted in <a href="http://msdn.microsoft.com/en-us/library/aa355056.aspx" rel="noreferrer">this MSDN article</a>, wrapping a WCF client in a <code>using</code> block could mask any errors that result in the client being left in a faulted state (like a timeout or communication problem). Long story short, when <code>Dispose()</code> is called, the client's <code>Close()</code> method fires, but throws an error because it's in a faulted state. The original exception is then masked by the second exception. Not good.</p>
  <p>The suggested workaround in the MSDN article is to completely avoid using a <code>using</code> block, and to instead instantiate your clients and use them something like this:</p>
  <pre><code>try
  {
      ...
      client.Close();
  }
  catch (CommunicationException e)
  {
      ...
      client.Abort();
  }
  catch (TimeoutException e)
  {
      ...
      client.Abort();
  }
  catch (Exception e)
  {
      ...
      client.Abort();
      throw;
  }
  </code></pre>
  <p>Compared to the <code>using</code> block, I think that's ugly. And a lot of code to write each time you need a client.</p>
  <p>Luckily, I found a few other workarounds, such as this one on the (now defunct) IServiceOriented blog. You start with:</p>
  <pre><code>public delegate void UseServiceDelegate&lt;T&gt;(T proxy); 
  
  public static class Service&lt;T&gt; 
  { 
      public static ChannelFactory&lt;T&gt; _channelFactory = new ChannelFactory&lt;T&gt;(&quot;&quot;); 
      
      public static void Use(UseServiceDelegate&lt;T&gt; codeBlock) 
      { 
          IClientChannel proxy = (IClientChannel)_channelFactory.CreateChannel(); 
          bool success = false; 
          try 
          { 
              codeBlock((T)proxy); 
              proxy.Close(); 
              success = true; 
          } 
          finally 
          { 
              if (!success) 
              { 
                  proxy.Abort(); 
              } 
          } 
       } 
  } 
  </code></pre>
  <p>Which then allows:</p>
  <pre><code>Service&lt;IOrderService&gt;.Use(orderService =&gt; 
  { 
      orderService.PlaceOrder(request); 
  }); 
  </code></pre>
  <p>That's not bad, but I don't think it's as expressive and easily understandable as the <code>using</code> block.</p>
  <p>The workaround I'm currently trying to use I first read about on <a href="http://blog.davidbarrett.net/archive/2007/11.aspx" rel="noreferrer">blog.davidbarret.net</a>. Basically, you override the client's <code>Dispose()</code> method wherever you use it. Something like:</p>
  <pre><code>public partial class SomeWCFServiceClient : IDisposable
  {
      void IDisposable.Dispose() 
      {
          if (this.State == CommunicationState.Faulted) 
          {
              this.Abort();
          } 
          else 
          {
              this.Close();
          }
      }
  }
  </code></pre>
  <p>This appears to be able to allow the <code>using</code> block again without the danger of masking a faulted state exception.</p>
  <p>So, are there any other gotchas I have to look out for using these workarounds? Has anybody come up with anything better?</p>