title: Implementing timeout for WCF service c# console app
body: <p>So I'm writing a console app that calls a WCF service (via nettcpbinding) - and the business wants to be able to specify a value to use as the timeout value.  I originally tried operation timeout but that seemed to get ignored - so I tried a bunch of other values.  One of those works (or combination of) :) </p>
  
  <p>I was hoping I could still close the channel if it times out - but if I put the closing in the finally block then it sits for another minute until it times out (whereas I'm setting a timeout value in seconds).  I'm testing by adding a delay in the server side code - to simulate it taking awhile.</p>
  
  <p>I can move the close to the first try block but I'm then concerned that I'll be leaving channels open.  It then will report back the timeout error to the user a lot quicker.  Or do I need to implement threading?</p>
  
  <pre><code>public static String ExecuteSearch(List&lt;KeyValuePair&lt;string, string&gt;&gt; paramItems)
  {
      var context = GetAdminContext();
      var parsedParameters = ParseParameters((paramItems));
  
      //TODO: Figure out how to implement timeout - &amp; workout if port number will be passed in?? 
      IPartyProfile partyProfile = null;
  
      long start = System.Environment.TickCount;
  
      using (ChannelFactory&lt;IPartyController&gt; factory = new ChannelFactory&lt;IPartyController&gt;("IPartyControllerEndpoint"))
      {
          EndpointAddress address = new EndpointAddress(String.Format("net.tcp://{0}/ServiceInterface/PartyController.svc", parsedParameters.HostName));
          IPartyController proxy = factory.CreateChannel(address);
  
          if (proxy != null)
          {
              var timeoutTimeSpan = new TimeSpan(0, 0, parsedParameters.TimeOut);
              ((IContextChannel)proxy).OperationTimeout = timeoutTimeSpan;
              factory.Endpoint.Binding.SendTimeout = timeoutTimeSpan;
              factory.Endpoint.Binding.ReceiveTimeout = timeoutTimeSpan;
              factory.Endpoint.Binding.OpenTimeout = timeoutTimeSpan;
              factory.Endpoint.Binding.CloseTimeout = timeoutTimeSpan;
  
              try
              {
                  // TODO: potentially call something more complex
                  partyProfile = proxy.GetLatestPartyProfile(context, parsedParameters.PartyId);
  
              }
              catch (EndpointNotFoundException ex)
              {
                  throw new Exception(STATUS_UNKNOWN + ": Endpoint specified not responding", ex);
              }
              catch (TimeoutException ex)
              {
                  throw new Exception(STATUS_UNKNOWN + ": Timeout exceeded", ex);
              }
              finally
              {
                  try
                  {
                      ((IClientChannel)proxy).Close();
                  }
                  catch (Exception)
                  {
                  }
  
              }
          }
      }
  
      long stop = System.Environment.TickCount;
      long elapsed = (stop - start) / 1000; // in seconds
  
      return SetResultMessage(elapsed, partyProfile, parsedParameters);
  }
  </code></pre>
  
  <p><strong>Edit - I think I can use factory.Abort() to end it quicker.  (I'd put it where the Close is in the above code).</strong></p>