Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

WCF RESTful on Windows Service Host

4.75/5 (4 votes)
14 Jul 2015CPOL2 min read 48.9K  
Hosting WCF REST Service on Windows Service

Introduction

This article/code will give you a working example of how to host a WCF RESTful service on a Windows Service. The code/solution is on Visual Studio 2015 RC but should work on the earlier relevant versions as well. The service provides both JSON and XML/Text response.

Caution

Open Visual Studio and Command Prompt as Administrator only. You won't be able to install your service on Windows Services if you don't open 'VS2013 x64 Cross Tools Command Prompt' as admin.

Image 1

Code

Below are the steps to develop the same demo...

Service

Create the WCF Service.

  1. Add a WCF Service Library to the solution and name it 'RestWCFServiceLibrary'.

    Image 2

  2. Add a new interface named 'IRestWCFServiceLibrary'.
    C#
    using System.ServiceModel;
    using System.ServiceModel.Web;
    
    namespace RestWCFServiceLibrary
    {
        [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);
        }
    }
  3. Add a new class named 'RestWCFServiceLibrary' which implements 'IRestWCFServiceLibrary'.
    C#
    namespace RestWCFServiceLibrary
    {
        public class RestWCFServiceLibrary : IRestWCFServiceLibrary
        {
            public string XMLData(string id)
            {
                return Data(id);
            }
            public string JSONData(string id)
            {
                return Data(id);
            }
    
            private string Data(string id)
            {
                // logic
                return "Data: " + id;
            }
        }
    }    
  4. Add/modify the 'App.Config' as below...

    Image 3

Host

Now we need to host our WCF Service. So create a Windows Service project and name it as 'RestWCFWinService'.

  1. Code in 'Program.cs' file...

    Debugging facility: The highlighted code in yellow can be uncommented and the project debugged right away!
    For this...

    • Uncommented the highlighted code.
    • Set 'RestWCFWinService' as startup project.
    • Put a debugging stop-point.
    • Add OnDebug() as mentioned in #3...
    • This code needs to be commented while building for the deployment.

    Image 4

  2. Create the installer file named 'MyRestWCFRestWinSerInstaller'. This file enables the .exe to be installed on Windows Services.

    Image 5

  3. Create a class file named 'MyRestWCFRestWinSer' with the below code. This class inherits 'ServiceBase'.

    Debugging facility:

    • OnDebug() is required for debugging as mentioned in #1...

    Image 6

    For the 'RestWCFServiceLibrary', add the reference...

    Likewise, references to System.ServiceModel, etc. would also be required.

    Image 7

  4. Add the App.config with the end points...

    Here, the service would be hosted on port 8888, you can change it with any not in use but valid port.

    Image 8

Installing the Host Application on Windows Services

  1. Build your solution and grab the path where "RestWCFWinService" generates the .exe file. In my case, it is at... '..\WcfRestfulWithWindowsHostDemo\WindowsService1\bin\Debug\WindowsService1.exe' of "RestWCFWinService" project.

    Info: WindowsService1 is the folder name of 'RestWCFWinService' project in Solution Explorer.

  2. Now open the Visual Studio Command Prompt in Administrator mode and install the application using 'installutil -i file_path', like this..

    Image 9

    You can uninstall the same application using 'installutil -u file_path',

  3. Once the installation is successful, go to RUN --> services.msc.

    Image 10

  4. Start the 'Demo' service...

    Image 11

  5. Now test the service by browsing the below URLs...

    Image 12

and you are done! :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)