Host WCF service in windows service

Kailash Chandra Behera | Tuesday, October 20, 2020


Here in this blog, we are going to discuss step by step how to host WCF in windows service. Here we will create a WCF self-hosted windows service that will host WCF service at run time. Let’s go to the getting started section for more details.

Getting Started

The following steps help to create WCF self-hosted windows service where the WCF service will be hosted at runtime, follow the steps.

Steps to host WCF in windows service

  1. Open your source code where you have written code for the WCF service.

  2. Then add a new windows service to your project solution, the below steps adds a widow service.

    1. Right-click on your project solution.

    2. Then go to “Add” and click on “New Project”.

    3. The “Add new project” will appear, in the search box search for “Windows Service” project template.

    4. Select the “Windows Service (.Net Framework)” project template, give a name and click on the “OK” button.

  3. On your windows service take reference to your WCF service.

  4. Open the service as code view by right-clicking on the service(service1.cs file) file and on “View Code”.

  5. Inside the OnStart method write the hosting code, see the hosting code example in the below of these steps.

  6. Now you are done with hosting WCF service in windows service.

  7. Build your windows service application in realease mode and prepare for installation

  8. See the related artcile secetion in the below, the list of blog links has been given that guides how to install windows service

WCF self hosting example

 // Create a URI to serve as the base address    
 //Address pattern protocol: hostname: port / Namespace / classname  
 Uri Url = new Uri("http://localhost:8090/HellowWorldService/HelloWorld");  
 //Create ServiceHost    
 ServiceHost host = new ServiceHost(typeof(HellowWorldService.IHelloWorldr), httpUrl);  
 //Add a service endpoint    
 host.AddServiceEndpoint(typeof(HellowWorldService.IHelloWorld), newWSHttpBinding(), "");  
 //Enable metadata exchange    
 ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();  
 behavior.HttpGetEnabled = true;  
 //Start the Service    

WCF self hosting example

In the above code example, the first line code is to create an object of Uri class to get the base address of your service, this class takes a string argument in his constructor. The string argument is the address pattern of your service like the following.

 Address pattern: protocol:hostname:port/Namespace/classname  

The second line of code creates an object of service host class that creates an environment for hosting WCF service. The remaining codes are is for setting the behavior of service.

To host your service you have to implement or use the following main 3 classes.

  1. Uri:

    This class is used to create a base address (URL) for your WCF service, Its comes under the system namespace. fore more details Click Here

  2. ServiceHost:

    Service host class is used to create or provides a host for your service and this class comes under System.ServiceModel namespace. Click here for more details

  3. ServiceMetadataBehavior:

    This class comes under System.ServiceModel.Description namespace and System.ServiceModel (in System.ServiceModel.dll) assembly. For more details click here.

Related Articles

  1. Install Uninstall Windows Service Using Batch File
  2. Install and Uninstall Windows Service


In the above of this blog, we saw how to host wcf service in windows service. I hope you have enjoyed it a lot.