title: Hosting WCF service via ServiceHost in Console/WinForms
body: <p>I'm hosting WCF service in console application (.NET 4.0). Service code (from <a href="http://msdn.microsoft.com/en-us/library/bb472488.aspx" rel="nofollow noreferrer">msdn</a> example):</p>
  
  <pre><code>using System.Runtime.Serialization;
  using System.ServiceModel;
  using System.ServiceModel.Web;
  
  namespace WCFServiceHost
  {
      [ServiceContract(Namespace = "WCFServiceHost")]
      public interface ICalculator
      {
          [WebInvoke(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
          MathResult DoMathJson(double n1, double n2);
  
          [WebInvoke(ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped)]
          MathResult DoMathXml(double n1, double n2);
  
      }
  
      public class CalculatorService : ICalculator
      {
  
          public MathResult DoMathJson(double n1, double n2)
          {
              return DoMath(n1, n2);
          }
  
          public MathResult DoMathXml(double n1, double n2)
          {
              return DoMath(n1, n2);
          }
  
          private MathResult DoMath(double n1, double n2)
          {
              MathResult mr = new MathResult();
              mr.sum = n1 + n2;
              mr.difference = n1 - n2;
              mr.product = n1 * n2;
              mr.quotient = n1 / n2;
              return mr;
          }
      }
  
      [DataContract]
      public class MathResult
      {
          [DataMember]
          public double sum;
  
          [DataMember]
          public double difference;
  
          [DataMember]
          public double product;
  
          [DataMember]
          public double quotient;
      }
  }
  </code></pre>
  
  <p>Next the console app code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.ServiceModel;
  
  namespace WCFServiceHost
  {
      class Program
      {
          public static void Main()
          {
              var adrs = new Uri[1];
              adrs[0] = new Uri("http://localhost:3980");
              using (ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService), adrs))
              {
                  try
                  {
                      // Open the ServiceHost to start listening for messages.
                      serviceHost.Open();
  
                      // The service can now be accessed.
                      Console.WriteLine("The service is ready.");
                      Console.WriteLine("Press &lt;ENTER&gt; to terminate service.");
                      Console.ReadLine();
  
                      // Close the ServiceHost.
                      serviceHost.Close();
                  }
                  catch (TimeoutException timeProblem)
                  {
                      Console.WriteLine(timeProblem.Message);
                      Console.ReadLine();
                  }
                  catch (CommunicationException commProblem)
                  {
                      Console.WriteLine(commProblem.Message);
                      Console.ReadLine();
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>And my 2 questions:</p>
  
  <p>1.<del> When I'm open _http://localhost:3980 I've get:</del>
  <img src="https://i.stack.imgur.com/vGcxh.png" alt="enter image description here"> <del><strong>How to enable metadata publishing?</strong></del> See Dai Bok answer. </p>
  
  <ol start="2">
  <li>Now how to get data from this service - (to get data from service like in <a href="http://msdn.microsoft.com/en-us/library/bb472488.aspx" rel="nofollow noreferrer">msdn</a> example)? Embed any web framework (like <a href="http://nancyfx.org/" rel="nofollow noreferrer">Nancy</a>) or use HttpListener?</li>
  </ol>