Hosting a WCF Service Using Windows Service Hosting


Shikha Gupta
SharePoint Developer
Published On :   05 Jul 2016
Visit Count
Today :  3    Total :   2009
Plan, Migrate, Secure, Report
SharePoint & Office 365 Tool. Simple & Easy to Use. 15-Day Trial!

Sharegate: Kick-Ass Tool
Think Your SharePoint & Office 365 Are Secure ? Find Out Now!


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=PC258762\MSSQLSERVER2008;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:

Alias

EmployeeService

Application Pool

.Net v4.5

Physical Path

C:\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

SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Categories

KWizCom Scan