title: Best Practice with WCF ChannelFactory and connection timeout
body: <p>I am working on an winform application that will access a WCF service self-hosted as a windows service. I am using the ChannelFactory instead of the service reference. I have been successful in connecting and calling the WCF service. The issue is when I let the application remain idle for 20 minutes and then try to make another call. I receive the following error:</p>
  
  <p>"The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue. Local socket timeout was '00:00:59.9489970'."</p>
  
  <p>I am looking for the best practice on managing the connection. I currently have created a function called PrepareWCFConnection (see below) that checks state of the channel and the ChannelFactory. I call this method before I make any calls to the WCF services. Is there a better way of handling this?</p>
  
  <pre><code>     public bool PrepareWCFConnection()
      {
          if ((channelFactory == null) || 
              (channelFactory.State == CommunicationState.Faulted) ||
              (channelFactory.State != CommunicationState.Opened))
          {
              channelFactory = new ChannelFactory&lt;IService1&gt;(new NetTcpBinding(), endpointAddress);
          }
  
  
          if ((proxy == null) ||
              (((IClientChannel)proxy).State == CommunicationState.Faulted) ||
              (((IClientChannel)proxy).State != CommunicationState.Opened))
          {
              proxy = channelFactory.CreateChannel(endpointAddress);
              ((IClientChannel)proxy).Open();
          }
  
          return true;
      }
  </code></pre>