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
 

Hosting a WCF Service Using Windows Service Hosting

Shikha Gupta
 
SharePoint Developer
 
Rate this article
 
Views
3685

We have created the WCF and have done self-hosting and Windows Service hosting and we can host the same WCF through IIS. Hosting through IIS is the most common way to do so. In order to do so follow the following steps and if you want to see how to create the WCF and the client application then click here.

1. Right click on EmployeeClassLibraryService Solution and click on add a website and then select WCF then click on browse to open the solution where you have created the EmployeeClassLibraryService Solution.

2. Once you have that click on open and for this website you can just add EmployeeServiceIISHost to its name.

3. Once you have the EmployeeServiceIIS host in the solution then expand the App_code and delete the IService.cs and Servcie.cs as this is for WCF service but we already have our WCF ready.

4. Right click on the website and click on add reference of EmployeeClassLibraryService project.

5. Rename Service.svc file to EmployeeService.svc and delete the existing code and copy paste the following code. The service attribute basically holds namespace.service name. We have remove the code behind tag because we don’t have the service in app_code folder we have deleted the services which we had in this folder.

<%@ ServiceHost Language="C#" Debug="true" Service="EmployeeClassLibraryService.EmployeeService" %>

6. Open the Web.config file and delete the configuration and copy and paste the following configuration. It is the same piece of code which we had in app.config file for self-hosting or web-service hosting.

 <?xml version="1.0" encoding="utf-8" ?>
 <configuration>
   <connectionStrings>
     <add name ="EmpCS" connectionString="Data Source=PC258762MSSQLSERVER2008;Initial Catalog=DbEmployee;Integrated Security=True" providerName="System.Data.SqlClient" />
   </connectionStrings>
   <system.serviceModel>
 
     <services>
 
       <service name="EmployeeClassLibraryService.EmployeeService" behaviorConfiguration="mexBehavior">
         <endpoint address="EmployeeService" binding="basicHttpBinding" contract="EmployeeClassLibraryService.IEmployeeService"></endpoint>
         <host>
           <baseAddresses>
             <add baseAddress="http://localhost:8090"/>
           </baseAddresses>
         </host>
       </service>
     </services>
     <behaviors>
       <serviceBehaviors>
         <behavior name="mexBehavior">
           <serviceMetadata httpGetEnabled="true"/>
         </behavior>
       </serviceBehaviors>
     </behaviors>
   </system.serviceModel>
 </configuration>
 

7. Build the solution.

8. Now we need to create a virtual directory in IIS. In the run window type intemgr this will open the IIS.

9. Expand Sites and in the default website right click on it and click on add application.

The details Entered is shown below:

AliasEmployeeService
Application Pool.Net v4.5
Physical PathC:\Users\430379\Documents\Visual Studio 2013\Projects\EmployeeClassLibraryService\EmployeeServiceIISHost

10. Once the application is added then right click on it and click on option switch to content view and then on the EmployeeService.svc right click on it and click on Browse.

11. Once you click on the browse button then it should open the following link

http://localhost/EmployeeService/EmployeeService.svc

12. You can also find the link of your wsdl document which might be a bit different in your case something like : http://pc258762.cts.com/EmployeeService/EmployeeService.svc?wsdl

13. Open the Employee Client application which we have created in the previous article. For more information on how to create client application which will consume this WCF click here.

Now in Employee Client Application delete the service reference which was previously added and delete the service reference folder as well.

14. Now add a new Service reference and copy and paste the wsdl document link

http://pc258762.cts.com/EmployeeService/EmployeeService.svc?wsdl and click go this will discover your service give a proper namespace like EmployeeService and click ok.

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

Save Employee:

Get Employee:

We have hosted the WCF in IIS without writing a single line of code for hosting as we had to do in Self hosting or Web Service Hosting. We had our service in the same application but we can even have it in different applications or in App_code folder.

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