Hanumantha
Hanumantha

Reputation: 117

Hosting WCF REST Service as Windows Service

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:

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;
        }
    }
}

RestWCFServiceLibrary.cs

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);
    }
}

App.config

    <?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.web>
    <compilation debug="true" />
  </system.web>
  <!-- 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. -->
  <system.serviceModel>
    <services>
      <service behaviorConfiguration="RestWCFServiceLibrary.Service1Behavior"
        name="RestWCFServiceLibrary.RestWCFServiceLibrary">
        <endpoint address="" binding="webHttpBinding"     contract="RestWCFServiceLibrary.IRestWCFServiceLibrary" behaviorConfiguration="web">
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8001/RestWCFServiceLibrary/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="RestWCFServiceLibrary.Service1Behavior">
          <!-- To avoid disclosing metadata information, 
      set the value below to false and remove the metadata endpoint above before     deployment -->
          <serviceMetadata httpGetEnabled="True"/>
          <!-- 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 -->
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
            <endpointBehaviors>
                <behavior name="web">
                    <webHttp/>
                </behavior>
            </endpointBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

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

MyRestWCFRestWinSer.cs

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;
            }
        }
    }
}

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;

namespace RestWCFWinService
{
    static class Program
    {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
        static void Main()
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
            { 
                new MyRestWCFRestWinSer() 
            };
            ServiceBase.Run(ServicesToRun);
        }
    }
}

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 "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"

But If I do this using SOAP it is working perfect.

So please anyone help me to install this REST WCF service as Windows service.

Upvotes: 4

Views: 8752

Answers (1)

Tim
Tim

Reputation: 28540

I believe there are two issues - one of which you have corrected per your comments.

First, you're using ServiceHost instead of WebServiceHost. I'm not 100% certain that's part of the problem, but based on your comments (no errors in the Event Viewer when using ServiceHost, error when you changed to WebServiceHost would seem to indicate it was).

The second issue appears to be related to your configuration file. You have a WCF service library (a DLL). By design, DLLs do not use the app.config file included in the project template - they use the config file of the consuming application. In this case, the Windows Service. Copy the <system.serviceModel> section from your library config file to the app.config file of your Windows service. Your WCF class library should pick up the endpoint at that point.

Note that the Windows Service config file, once the project is compiled, will be named MyRestWCFRestWinSer.exe.config, not App.config.

Upvotes: 5

Related Questions