title: C# WCF closing channels and using functions Func<T>
body: <p>This is the point, I have a WCF service, it is working now. So I begin to work on the client side. And when the application was running, then an exception showed up: timeout. So I began to read, there are many examples about how to keep the connection alive, but, also I found that the best way, is create channel, use it, and dispose it. And honestly, I liked that. So, now reading about the best way to close the channel, there are two links that could be useful to anybody who needs them:</p>
  
  <p><a href="http://geekswithblogs.net/SoftwareDoneRight/archive/2008/05/23/clean-up-wcf-clients--the-right-way.aspx" rel="nofollow noreferrer"> 1. Clean up clients, the right way</a></p>
  
  <p><a href="http://www.atrevido.net/blog/2007/08/13/Practical+Functional+C+Part+II.aspx" rel="nofollow noreferrer">2. Using Func</a></p>
  
  <p>In the first link, this is the example:</p>
  
  <pre><code>    IIdentityService _identitySvc;
  ...
  if (_identitySvc != null)
   {
       ((IClientChannel)_identitySvc).Close();
       ((IDisposable)_identitySvc).Dispose();
       _identitySvc = null;
   }
  </code></pre>
  
  <p>So, if the channel is not null, then is closed,  disposed, and assign null. But I have a little question. In this example the channel has a .Close() method, but, in my case, intellisense is not showing a Close() method. It only exists in the factory object. So I believe I have to write it. But, in the interface that has the contracts or the class that implemets it??. And, what should be doing this method??. </p>
  
  <p>Now, the next link, this has something I haven't try before. <code>Func&lt;T&gt;</code>. And after reading the goal, it's quite interesting. It creates a funcion that with lambdas creates the channel, uses it, closes it, and dipose it. This example implements that function like a <code>Using()</code> statement. It's really good, and a excellent improvement. But, I need a little help, to be honest, I can't understand the function, so, a little explanatino from an expert will be very useful. This is the function:</p>
  
  <pre><code>TReturn UseService&lt;TChannel, TReturn&gt;(Func&lt;TChannel, TReturn&gt; code)
  {
      var chanFactory = GetCachedFactory&lt;TChannel&gt;();
      TChannel channel = chanFactory.CreateChannel();
      bool error = true; 
      try {
          TReturn result = code(channel); 
          ((IClientChannel)channel).Close();
          error = false; 
          return result; 
      }
      finally {
          if (error) {
              ((IClientChannel)channel).Abort();
          }
      }
  }
  </code></pre>
  
  <p>And this is how is being used:</p>
  
  <pre><code>int a = 1; 
  int b = 2; 
  int sum = UseService((ICalculator calc) =&gt; calc.Add(a, b)); 
  Console.WriteLine(sum);
  </code></pre>
  
  <p>Yep, I think is really, really good, I'd like to understand it to use it in the project I have.</p>
  
  <p>And, like always, I hope this could be helpful to a lot of people.</p>