WCF Windows Service Hosting - Windows Communication Foundation (WCF)

What is WCF Windows Service Hosting?

The operation of Windows service hosting is a simple one. Given beneath are the steps with requisite coding and screenshots that explain the process in an easy way.

Step-1: Now let’s create a WCF service. Open Visual Studio 2008 and click New → Project and select Class Library from the template.

wcf 1

Step-2: Add reference System.ServiceModel to the project. This is the core assembly used for creating the WCF service.

Step-3: Next, we can create the ISimpleCalulator interface. Add the Service and Operation Contract attribute as shown beneath:

Step-4: Implement the ISimpleCalculator interface as shown beneath:

Step-5: Build the Project and get the dll. Now, we are ready with the WCF service. We are going to see how to host the WCF service in Windows service.

Note: In this project, it is mentioned that we are creating both Contract and Service (implementation) in the same project. However it is always a good practice if you have both in different projects.

Step-6: Open Visual Studio 2008 and Click New → Project and select Windows Service.

wcf 2

Step-7: Add 'WindowsServiceHostedService.dll' as reference to the project. This assembly is going to act as service.


Step-8: The OnStart method of the service can be used to write the hosting code for WCF. We have to make sure that we are using only one service host object. OnStop method is used to close the Service Host. The subsequent code shows how to host the WCF service in Windows service.

Step-9: In order to install the service, we need to have the Installer class for the Windows service. So add a new Installer class to the project, which is inherited from the Installer class. Given beneath is the code that shows the Service name, StartUp type, etc. of the service.

Step-10: Build the project to get the executable file WCFHostedWindowsService.exe. Next, we need to install the service using the Visual Studio Command Prompt. So open the command prompt by clicking Start→All Programs→Microsoft Visual Studio 2008→Visual Studio Tools→ Visual Studio Command Prompt. Using the install util utility application, you can install the service as shown beneath.


All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Windows Communication Foundation (WCF) Topics