Hosting a WCF service in Windows service – Easy WCF
Skip to content

Hosting a WCF service in Windows service

Before hosting WCF service in WIndows service , you should understand, What is Windows Service?

Let’s create a Windows service and add it to WCFSelfHostService solution.

  • Right click on solution–>add–>New Project–>Rename it to WindowsServiceHost.
  • Add reference to WCFSelfHostService
  • Add reference to System.ServiceModel
  • Add new configuration file to WindowsServiceHost–>Right click on WindowsServiceHost–>Add app configuration file
  • We will copy the configuration from Host to WindowsServiceHost app.config
  • Rename service1.cs to WCFWindowsService.cs

  • Then go to design–>Right click–>Add Installer
  • serviceInstaller1–>Right click–>go to properties–>specify the service name as WCFWindowsService and make the start type to Automatic
  • Right click on serviceProcessInstaller1 from designer and go to properties and select Account as local system
  • Build the solution
    Start–>All programs–>visual studio–>visual studio tool–>visual studio command prompt
    install – i
  • You can get the exe path from right click on WindowsServiceHost–>Open window in file explorer–>bin–>debug and windowsHostService.exe
    hit enter and the install would be completed
  • then go to Run–>services.msc and in services windows you will see the installed WindowsHostService and you can start it manually
  • To uninstall you can use option -u
  • Now you can open the WCFSelfHostCLient and update service reference and run the client to test if the service is hosted correctly or not.

Advantages of hosting WCF service in windows service:

  • Windows service runs in background and can start Automatically
  • In case of failure,Windows service can be configured to restart automatically
  • Windows service supports all binding and transport protocol
  • The WCF service that the windows service hosts starts automatically or you can manually start or stop the service

Disadvantages of hosting WCF service in windows service:

  • For Windows service, you need to write custom code
  • Windows service that host WCF should be deployed to the environment you are hosting the service
  • Windows service supports all binding and transport protocol
  • You need to attach a separate process where the windows service is running
  • DIfficult to debug due to separate process

© 2015, admin. All rights reserved.

One thought on “Hosting a WCF service in Windows service

  1. Pingback: Top 10 WCF Interview Questions | EasyWCF

Leave a Reply

Your email address will not be published. Required fields are marked *