Click here to Skip to main content
15,887,376 members
Articles / Programming Languages / C#
Tip/Trick

WCF RESTful on Windows Service Host

Rate me:
Please Sign up or sign in to vote.
4.75/5 (4 votes)
14 Jul 2015CPOL2 min read 47.8K   8   6
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)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionI got error CORS when running your code. Pin
Member 118966838-May-23 22:04
Member 118966838-May-23 22:04 
GeneralMy vote of 5 Pin
Member 1461848822-Jul-20 23:21
Member 1461848822-Jul-20 23:21 
QuestionInitializeComponent() Pin
Member 1414524627-Feb-19 1:49
Member 1414524627-Feb-19 1:49 
QuestionGreat article and good working demo ! Pin
Member 1283185723-Mar-17 21:11
Member 1283185723-Mar-17 21:11 
PraiseThanks! Pin
Member 1069281719-Jul-16 22:36
Member 1069281719-Jul-16 22:36 
QuestionAll images are broken Pin
Tridip Bhattacharjee14-Jul-15 21:39
professionalTridip Bhattacharjee14-Jul-15 21:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.