WCF Service Hosting – Windows Service – Part II

Shikha Gupta
 
SharePoint Developer
July 5, 2016
 
Rate this article
 
Views
3569

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" ?>
 <configuration>
   <connectionStrings>
     <add name ="EmpCS" connectionString="Data Source=GBRDCSPSDEV05;Initial Catalog=DbEmployee;Integrated Security=True" providerName="System.Data.SqlClient" />
   </connectionStrings>
   <system.serviceModel>
 
     <services>
       <service name="EmployeeClassLibraryService.EmployeeService" behaviorConfiguration="mexBehavior">
         <endpoint address="EmployeeService" binding="netTcpBinding" contract="EmployeeClassLibraryService.IEmployeeService"></endpoint>
         <host>
           <baseAddresses>
             <add baseAddress="http://localhost:8090"/>
             <add baseAddress="net.tcp://localhost:9000"/>
           </baseAddresses>
         </host>
       </service>
     </services>
     <behaviors>
       <serviceBehaviors>
         <behavior name="mexBehavior">
           <serviceMetadata httpGetEnabled="true"/>
         </behavior>
       </serviceBehaviors>
     </behaviors>
   </system.serviceModel>
 </configuration>
 
 

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));

host.Open();

10. Under OnStop method copy the following code.

host.Close();

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

Category : Windows

Author Info

Shikha Gupta
 
SharePoint Developer
 
Rate this article
 
Sikha has been working in IT Industry for over 5 years. She holds a B-tech degree. She is passionate about learning and sharing the tricks and tips in .Net , ...read more
 

Leave a comment