title: Create a self hosted WCF Service inside a Windows Form
body: <p>If I use this code for self Host a WCF service in a Console application it works. I run the host app and then from another app (which I call the client app,) I can add the service reference from visual studio > solution explorer > ADD SERVICE REFERENCE > <a href="http://10.131.131.14:8080/sendKioskMessage" rel="nofollow noreferrer">http://10.131.131.14:8080/sendKioskMessage</a> > click GO, add the service with no problems and consume it from the client app (which is a windows form)</p>
  
  <p>But if I run the same code in a Windows Form, I run first the (SELF HOST WCF) windows form app, then from the other app (client app) in visual studio I try to add the service reference from ADD SERVICE REFERENCE in solution explorer (Just the same way that it works before but with the Console App self host) but it throws the following error:</p>
  
  <p>*</p>
  
  <blockquote>
    <p>An error (Details) occurred while attempting to find services at 
    <a href="http://10.131.131.14:8080/sendKioskMessage" rel="nofollow noreferrer">http://10.131.131.14:8080/sendKioskMessage</a>.</p>
    
    <p>(If I click Details Link, says the following:)</p>
    
    <p>There was an error downloading
    '<a href="http://10.131.131.14:8080/sendKioskMessage/" rel="nofollow noreferrer">http://10.131.131.14:8080/sendKioskMessage/</a>$metadata'. Unable to
    connect to the remote server. Metadata contains a reference that
    cannot be resolved: '<a href="http://10.131.131.14:8080/sendKioskMessage" rel="nofollow noreferrer">http://10.131.131.14:8080/sendKioskMessage</a>'.
    There was no endpoint listening at
    <a href="http://10.131.131.14:8080/sendKioskMessage" rel="nofollow noreferrer">http://10.131.131.14:8080/sendKioskMessage</a> that could accept the
    message. This is often caused by an incorrect address or SOAP action.
    See InnerException, if present, for more details. Unable to connect to
    the remote server. If the service is defined in the current solution,
    try building the solution and adding the service reference again.</p>
  </blockquote>
  
  <p>*</p>
  
  <p>The IP that I use is the IP of my pc where both apps are running. I also used localhost instead of my actual IP with the same result.</p>
  
  <p>Windows Form Code (can't add the service from another app):</p>
  
  <pre><code>public partial class KioskosServerForm : Form
      {
  
  
          [ServiceContract]
          public interface IKioskMessageService
          {
              [OperationContract]
              string SendKioskMessage(string message);
          }
  
          public class KioskMessageService : IKioskMessageService
          {
              public string SendKioskMessage(string message)
              {
                  return string.Format("Message sent: {0}", message);
              }
          }
  
          public KioskosServerForm()
          {
              InitializeComponent();
          }
  
          private void KioskosServerForm_Load(object sender, EventArgs e)
          {
              Uri baseAddress = new Uri("http://10.131.131.14:8080/sendKioskMessage");
  
              try
              {
                  // Create the ServiceHost.
                  using (ServiceHost host = new ServiceHost(typeof(KioskMessageService), baseAddress))
                  {
                      // Enable metadata publishing.
                      ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                      smb.HttpGetEnabled = true;
                      smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
                      host.Description.Behaviors.Add(smb);
  
                      // Open the ServiceHost to start listening for messages. Since
                      // no endpoints are explicitly configured, the runtime will create
                      // one endpoint per base address for each service contract implemented
                      // by the service.
                      host.Open();
                  }
              }
              catch (Exception exp)
              {
                  MessageBox.Show(exp.InnerException.Message);
              }
          }
      }
  </code></pre>
  
  <p>Console App Code (Works! I can add the service from other client app):</p>
  
  <pre><code>[ServiceContract]
  public interface IKioskMessageService
  {
      [OperationContract]
      string SendKioskMessage(string message);
  }
  
  public class KioskMessageService : IKioskMessageService
  {
      public string SendKioskMessage(string message)
      {
          return string.Format("Message sent: {0}", message);
      }
  }
  
  class Program
  {
      static void Main(string[] args)
      {
          Uri baseAddress = new Uri("http://localhost:8080/sendKioskMessage");
  
          // Create the ServiceHost.
          using (ServiceHost host = new ServiceHost(typeof(KioskMessageService),baseAddress))
          {
              // Enable metadata publishing.
              ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
              smb.HttpGetEnabled = true;
              smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
              host.Description.Behaviors.Add(smb);
  
              // Open the ServiceHost to start listening for messages. Since
              // no endpoints are explicitly configured, the runtime will create
              // one endpoint per base address for each service contract implemented
              // by the service.
              host.Open();
  
              Console.WriteLine("The service is ready at {0}", baseAddress);
              Console.WriteLine("Press &lt;Enter&gt; to stop the service.");
              Console.ReadLine();
  
              // Close the ServiceHost.
              host.Close();
          }
      }
  }
  </code></pre>
  
  <p>I don't know why I can consume the service if the service is self hosted in a console app, but I can't add it if the service is self hosted in a Windows Form.</p>
  
  <p>I will appreciate a lot your help to achieve this from a Windows From, since I need to self host the WCF service from a windows form, no a console app.</p>
  
  <p>I'm using Visual Studio 2017, .Net Framework 4.6.1</p>
  
  <p>THANKS IN ADVANCE GUYS!!</p>