title: How to consume a wcf service in a hostconsole application from client console app
body: <p>How to consume a wcf service in a hostconsole application from client console app.
  Here is the wcf service:</p>
  
  <pre><code>namespace HostConsoleApplication
  {
      class Program
      {
          static void Main(string[] args)
          {
              using (System.ServiceModel.ServiceHost host = new System.ServiceModel.ServiceHost(typeof(FirstWcfService.Service)))
              {
                  host.Open();
                  Console.WriteLine("Sai");
                  Console.ReadLine();
              }
          }
      }
  }
  </code></pre>
  
  <hr>
  
  <p>Sky, Thank you once more. You have read my mind.</p>
  
  <p>However i am running into an issue:</p>
  
  <p>Here is how i have modified the above code for the host and the client:</p>
  
  <p>The host</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.ServiceModel;
  
  namespace ConsoleApplication1
  {
      class Program
      {
  
          private static void Main(string[] args)
          {
              const string endpointAddress = "net.tcp://localhost:8001/Service";
  
              var baseAddress = new Uri(endpointAddress);
  
              using (var host = new ServiceHost(typeof(FirstWcfService.Service), baseAddress))
              {
                  host.AddServiceEndpoint(typeof(FirstWcfService.IService), new NetTcpBinding(), baseAddress);
                  host.Open();
  
              }
  
              Console.WriteLine("Sai");
              Console.ReadLine();
          }
  
      }
  }
  </code></pre>
  
  <p>The Client</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  
  using System.ServiceModel;
  using System.ServiceModel.Channels;
  using System.ServiceModel.Description;
  
  namespace ConsoleApplication2
  {
      class Program
      {
          private static void Main(string[] args)
          {
              const string endpointAddress = "net.tcp://localhost:8001/Service";
  
  
                  // this is the client code.. simply put this in another console app
                  using (var factory = new ChannelFactory&lt;FirstWcfService.IService&gt;(new NetTcpBinding(), endpointAddress))
                  {
                      FirstWcfService.IService channel = factory.CreateChannel();
  
                      //DateTime d = channel.GetDate();
                      string h = channel.Hello();
  
                      //Console.WriteLine(String.Format("It is {0}", d));
                      Console.WriteLine(String.Format("It is {0}", h));
                  }
                  //// end client code
  
          }
  
  
      }
  }
  </code></pre>
  
  <p>The host runs fine, when the client is ran here is the exception i am getting:</p>
  
  <p>Could not connect to net.tcp://localhost:8001/Service. The connection attempt lasted for a time span of 00:00:00.8899011. TCP error code 10061: No connection could be made because the target machine actively refused it 127.0.0.1:8001.</p>
  
  <p>Please let me know if you have any suggestions.
  Thanks
  N</p>