title: Hosting WCF REST Service as Windows Service
body: <p>I want to create REST WCF Service and Install it as Windows service.
  I have created REST WCF Service and I ran that, it is working fine for both xml amd json. 
  Below are the code files.
  IRestWCFServiceLibrary.cs:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Runtime.Serialization;
  using System.ServiceModel;
  using System.Text;
  
  namespace RestWCFServiceLibrary
  {
      // NOTE: If you change the class name "Service1" here, you must also update the             reference to "Service1" in App.config.
      public class RestWCFServiceLibrary : IRestWCFServiceLibrary
      {
          public string XMLData(string id)
          {
              return "Id:" + id;
          }
          public string JSONData(string id)
          {
              return "Id:" + id;
          }
      }
  }
  </code></pre>
  
  <p>RestWCFServiceLibrary.cs</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Runtime.Serialization;
  using System.ServiceModel;
  using System.Text;
  using System.ServiceModel.Web;
  
  namespace RestWCFServiceLibrary
  {
      // NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in App.config.
      [ServiceContract]
      public interface IRestWCFServiceLibrary
      {
          [OperationContract]
          [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "xml/{id}")]
          string XMLData(string id);
  
          [OperationContract]
          [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "json/{id}")]
          string JSONData(string id);
      }
  }
  </code></pre>
  
  <p>App.config</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;configuration&gt;
    &lt;system.web&gt;
      &lt;compilation debug="true" /&gt;
    &lt;/system.web&gt;
    &lt;!-- When deploying the service library project, the content of the config file must     be added to the host's 
    app.config file. System.Configuration does not support config files for libraries. --&gt;
    &lt;system.serviceModel&gt;
      &lt;services&gt;
        &lt;service behaviorConfiguration="RestWCFServiceLibrary.Service1Behavior"
          name="RestWCFServiceLibrary.RestWCFServiceLibrary"&gt;
          &lt;endpoint address="" binding="webHttpBinding"     contract="RestWCFServiceLibrary.IRestWCFServiceLibrary" behaviorConfiguration="web"&gt;
          &lt;/endpoint&gt;
          &lt;endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /&gt;
          &lt;host&gt;
            &lt;baseAddresses&gt;
              &lt;add baseAddress="http://localhost:8001/RestWCFServiceLibrary/" /&gt;
            &lt;/baseAddresses&gt;
          &lt;/host&gt;
        &lt;/service&gt;
      &lt;/services&gt;
      &lt;behaviors&gt;
        &lt;serviceBehaviors&gt;
          &lt;behavior name="RestWCFServiceLibrary.Service1Behavior"&gt;
            &lt;!-- To avoid disclosing metadata information, 
        set the value below to false and remove the metadata endpoint above before     deployment --&gt;
            &lt;serviceMetadata httpGetEnabled="True"/&gt;
            &lt;!-- To receive exception details in faults for debugging purposes, 
            set the value below to true.  Set to false before deployment 
            to avoid disclosing exception information --&gt;
            &lt;serviceDebug includeExceptionDetailInFaults="False" /&gt;
          &lt;/behavior&gt;
        &lt;/serviceBehaviors&gt;
              &lt;endpointBehaviors&gt;
                  &lt;behavior name="web"&gt;
                      &lt;webHttp/&gt;
                  &lt;/behavior&gt;
              &lt;/endpointBehaviors&gt;
      &lt;/behaviors&gt;
    &lt;/system.serviceModel&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>Now I want to host/install it as Windows Service, for that I added Window Service project and gave the reference of the RESR WCF which is created as above. Named the service class as MyRestWCFRestWinSer</p>
  
  <p>MyRestWCFRestWinSer.cs</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Diagnostics;
  using System.Linq;
  using System.ServiceProcess;
  using System.Text;
  using System.ServiceModel;
  using RestWCFServiceLibrary;
  
  namespace RestWCFWinService
  {
      public partial class MyRestWCFRestWinSer : ServiceBase
      {
          ServiceHost oServiceHost = null;
          public MyRestWCFRestWinSer()
          {
              InitializeComponent();
          }
  
          protected override void OnStart(string[] args)
          {
              oServiceHost = new ServiceHost(typeof(MyRestWCFRestWinSer));
              oServiceHost.Open();
          }
  
         protected override void OnStop()
          {
              if (oServiceHost != null)
              {
                  oServiceHost.Close();
                  oServiceHost = null;
              }
          }
      }
  }
  </code></pre>
  
  <p>Program.cs</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.ServiceProcess;
  using System.Text;
  
  namespace RestWCFWinService
  {
      static class Program
      {
      /// &lt;summary&gt;
      /// The main entry point for the application.
      /// &lt;/summary&gt;
          static void Main()
          {
              ServiceBase[] ServicesToRun;
              ServicesToRun = new ServiceBase[] 
              { 
                  new MyRestWCFRestWinSer() 
              };
              ServiceBase.Run(ServicesToRun);
          }
      }
  }
  </code></pre>
  
  <p>And I added project installer, I ran the installer. After running I registered the service from command prompt by using installutil.
  Service successfully registered and listed in the Services.
  If I start the service it is giving error as "<strong>The RestWCFWinService Service on Local Computer started and the stopped. Some services stop automatically if they are not in use by other services or programs</strong>"</p>
  
  <p>But If I do this using SOAP it is working perfect.</p>
  
  <p>So please anyone help me to install this REST WCF service as Windows service.</p>