Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Part 27 - Hosting a wcf service in a windows service

Suggested Videos
Part 24 - Self hosting a wcf service in console application
Part 25 - Self hosting a wcf service in winforms application
Part 26 - What is a windows service

In this video we will discuss, hosting a wcf service in a windows service. This is continuation to Part 26. Please watch Part 26, before proceeding. We will be working with the same example, that we worked with in Part 25.

Steps to create a windows service and host wcf service
Step 1: Right click on HelloService solution in solution explorer and select Add - New Project.

Step 2: From the Add New Project dialog box, select "Windows" under "Installed Templates". Select "Windows Service" project template. Type "WindowsServiceHost" in the "Name" textbox.

Step 3: Rename Service1.cs file to "HelloWindowsService.cs" and press "Enter" key. This will prompt you to change all references of "Service1". Click "Yes".

Step 4: Right click on "References" folder under "WindowsServiceHost" project and add a reference to System.ServiceModel assembly and HelloService project.

Step 5: Right click on WindowsServiceHost project in solution explorer, and add the Application Configuration file. This should add App.config file. Copy and paste the following configuration in App.config file
<?xml version="1.0" encoding="utf-8" ?>
                <behavior name="mexBehavior">
                    <serviceMetadata httpGetEnabled="true" />
            <service behaviorConfiguration="mexBehavior"
                <endpoint address="HelloService" binding="netTcpBinding"
                    contract="HelloService.IHelloService" />
                        <add baseAddress="http://localhost:8080" />
                        <add baseAddress="net.tcp://localhost:8090" />


Step 6: Right click on "HelloWindowsService.cs" file and select "View Code" option from the context menu.

Step 7: Copy and paste the following code in HelloWindowsService.cs file.
using System.ServiceModel;
using System.ServiceProcess;

namespace WindowsServiceHost
    public partial class HelloWindowsService : ServiceBase
        ServiceHost host;
        public HelloWindowsService()

        protected override void OnStart(string[] args)
            host = new ServiceHost(typeof(HelloService.HelloService));

        protected override void OnStop()


Step 8: Double click on "HelloWindowsService.cs" file under "WindowsServiceHost" project. Right click on the Design surface of "HelloWindowsService.cs" file and select "Add Installer" option from the context menu. This should add "ProjectInstaller.cs" file.

Step 9: Right click on "serviceInstaller1" on "ProjectInstaller.cs" and select "Properties" from the context menu. Set
Name = HelloWindowsService
StartType = Automatic

Step 10: Right click on "serviceProcessInstaller1" on "ProjectInstaller.cs" and select "Properties" from the context menu. Set
Account = Network Service

Step 11: Build the solution

Step 12: Let's install the HelloWindowsService now. Open Visual Studio Command Prompt as an administrator.

Step 13: Type the following command and press enter
installutil -i C:\HelloService\WindowsServiceHost\bin\Debug\WindowsServiceHost.exe

Step 14: At this point we should have "HelloWindowsService" installed. Now open run window by pressing "Windows + R" keys. Type "services.msc" and press enter. In the "Services" window, right click on "HelloWindowsService" and select "Start" from the context menu. This should start the windows service.

Step 15: Open "WindowsClient" project. Right click on "HelloService" under "Service References" folder and select "Update Service Reference" option. Run the client application and test it.

wcf tutorial


  1. Exception occurred while initializing the installation:
    System.IO.FileNotFoundException: Could not load file or assembly 'file:///E:\VB'
    or one of its dependencies. The system cannot find the file specified..

    please me related this problem. Ijaz Ali Shah

  2. I'm also getting same error any one find out any soluation

  3. It probably did not install because your path has spaces in it. Simply put "" marks around your string like this.
    installutil -i "C:\HelloService\WindowsServiceHost\bin\Debug\WindowsServiceHost.exe"


If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.