SharePoint Pals
 | Sign In
WCF Service Hosting – Windows Service – Part II
by Shikha Gupta 5 Jul 2016
SharePoint Developer
Today  :   3     Total  :    1012

The previous article was a demonstration of self-hosting a WCF service through console application. In order to see how to create a simple WCF and self-host it through console application click on this LINK.

Now the same WCF service can be hosted through windows service. Windows service is an application running on windows machine. It can start up automatically and does not need user intervention to start it. Advantage of hosting a WCF service through windows service is that we can configure the windows service to start automatically when the computer starts. This makes our WCF service always available to clients to consume even if no one is logged on to that computer.

Let us continue with the previous example in that we first created the class library project and then added a WCF to it. In order to host the WCF we created the console application and added the required configuration in app.config file. In another instance of visual studio we created the client application which will consume this WCF.

So I am continuing with the same project as earlier. We just need to add another host to it and make necessary changes.

1. In the EmployeeClassLibrary Service solution add a new web service project and name it WindowsServiceHost.

2. In EmployeeServiceHost first we have to add two references one of EmployeeClassLibraryService and the other of System.ServiceModel assembly.

3. Add an Application configuration file in WindowsServiceHost .

4. Remove the existing code from app.config file and copy and paste the following code in app.config file. It is same app.config file code as before.

 <?xml version="1.0" encoding="utf-8" ?>
     <add name ="EmpCS" connectionString="Data Source=GBRDCSPSDEV05;Initial Catalog=DbEmployee;Integrated Security=True" providerName="System.Data.SqlClient" />
       <service name="EmployeeClassLibraryService.EmployeeService" behaviorConfiguration="mexBehavior">
         <endpoint address="EmployeeService" binding="netTcpBinding" contract="EmployeeClassLibraryService.IEmployeeService"></endpoint>
             <add baseAddress="http://localhost:8090"/>
             <add baseAddress="net.tcp://localhost:9000"/>
         <behavior name="mexBehavior">
           <serviceMetadata httpGetEnabled="true"/>

5. Rename service1.cs file to EmployeeWindowsService.cs file and click yes on the prompt.

6. Right click on EmployeeWindowsService.cs file and click on view code.

7. In EmployeeWindowsService.cs file include the following namespace

using System.ServiceModel;

8. Create an instance under of ServiceHost class under EmployeeWindowsService class.

ServiceHost host;

9. Under OnStart method copy the following code.

host = new ServiceHost(typeof(EmployeeClassLibraryService.EmployeeService));


10. Under OnStop method copy the following code.


11. Go to EmployeeWindowsService.cs right click on the designer and then click on Add Installer. This will add ProjectInstaller.cs file which will contain ServiceProcessInstaller1 and ServcieInstaller1.

12. Right click on ServiceInstaller1 and click on properties. Give the service name as EmployeeWindowsService and StartType as Automatic.

13. Right click on ServiceProcessInstaller1 and click on properties. Give the Account as LocalSystem and there are other options for it as well which you can give according to your needs.

14. Build the solution.

15. Now the hosting is done we have to install the Employee windows service.

i. In start menu under visual studio tools go o Visual studio command prompt and run that as an administrator.

ii. Copy and paste the following command in the prompt window.

installutil -i D:\POCS\EmployeeClassLibraryService\WindowsServiceHost\bin\Debug\WindowsServiceHost.exe

*Note- the command is installutil -i path of your WindowsServiceHost.exe file. It will be different for you. In order to find it out. Right click on the WindowsServiceHost project and click on Open folder in file explorer and go to bin folder and then debug folder and there you will find WindowsServiceHost.exe

iii. In order to check the EmployeeWindowsService has been installed. In the run window type Services.msc and you will find the EmployeeWindowService. Since the system is not rebooted the service is not started automatically though the start type is automatic so right click on it and click on start.

iv. Now the windows service is started.

16. Now the client has to consume this WCF through windows service hosting. So open the client application and right on the EmployeeService reference and click on update the service reference.

17. Now Run the client application and test both the functionality

Save Employee:

Get Employee:

*Note- After the application is created and you have completed all the task then you might want to remove the Employee Windows Service from the services then run the following command in the comand prompt of visual studio.

installutil –u D:\POCS\EmployeeClassLibraryService\WindowsServiceHost\bin\Debug\WindowsServiceHost.exe

Happy Coding J

blog comments powered by Disqus

SharePoint Pals

SharePoint Pals, a community portal for SharePoint developers, Administrators and End Users. Let's join hands and share the point together.
Read this on mobile


Angular Js Training In Chennai
Advanced Angular Js training with real world developer scenarios
Angular Js, Web Api and Ionic for .Net Developers
All in one client side application development for .Net developers
Angular Js For SharePoint Developers
Get ready for the future. Its no more just C#

Get Connected

SharePoint Resources

SharePoint 2013 and 2010 Web Parts
Free Web Parts with Source Code for SharePoint Community

SharePoint 2013 Books and Tutorials
Collection of free SharePoint 2013 books and tutorials (eBooks, pdfs)

Supported By

Contribute your article and be eligible for a one month Free Subscription for Plural Sight. The Author of the most popular New Article (published in the previous month) will be awarded with a Free One month Plural Sight Subscription. Article can be sent to in a word document.

Related Resources

Recent Tweets

Twitter October 23, 22:21
How to Add/Remove User Custom Actions (in Site Actions Menu) Programmatically using CSOM PNP in SharePoint -

Twitter October 21, 21:34
How to Add a JS Link Reference to the Display Form or Any other ASPX Programmatically using CSOM PNP in SharePoint -

Twitter October 20, 13:01
How to Add a JS Link Reference to the NewForm and EditForm Programmatically using CSOM PNP in SharePoint Office 365-

Twitter October 12, 12:15
How to Deploy Provider Hosted Apps (Add-Ins) by App Stapling in SharePoint Office 365 -

Twitter October 11, 13:39
How to Deploy Provider HostedApp programmatically using CSOM in SharePoint Office 365 Activating Developer Feature -

Follow us @SharePointPals
Note: For Customization and Configuration, CheckOutRecent Tweets Documentation