Hosting WCF Service Inside IIS

Windows Communication Foundation

 
Windows Communication Foundation(WCF) takes many existing communication technologies, such as Web Services, Windows Remoting, Microsoft Message Queuing, and abstracts them into a single technology. In most cases, this simplifies the way you communicate with other applications. It also allows you to communicate with other applications without being coupled to a specific technology. In this, we are going to create a simple WCF service and we are going to host that service inside SharePoint 2010.
 

Create a WCF Service

 
The following steps should be followed to create a WCF service using Visual Studio 2010.
 
I. Open Visual Studio 2010.
 
ii. Go to File => New => Project.
 
iii. Select WCF Service Application under installed template category WCF and name it as Wcf.
 
image1.gif 
 
iv. Target the .Net Framework 3.5.
 
v. Click OK.
 
vi. In the Solution Explorer delete Service.svc and IService.cs files.
 
vii. Right click the Solution Explorer and add a New Item.
 
viii. Select WCF Service and name it as CustomService.svc.
 
image2.gif 
 
ix. Replace ICustomerService.cs with the following code.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.Text;   
  7.   
  8. namespace Wcf  
  9. {  
  10.     [ServiceContract]  
  11.     public interface ICustomService  
  12.     {  
  13.         [OperationContract]  
  14.        string GetTime();  
  15.     }  
  16. }  
x. Replace CustomerService.svc.cs with the following code.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.Text;  
  7.   
  8. namespace Wcf  
  9. {  
  10.     public class CustomService : ICustomService  
  11.     {  
  12.         public string GetTime()  
  13.         {  
  14.             return DateTime.Now.ToString();  
  15.         }  
  16.     }  
  17. }  
xi. In the Web.config file remove the below code.
  1. <service behaviorConfiguration="Wcf.Service1Behavior" name="Wcf.Service1">  
  2.         <endpoint address="" binding="basicHttpBinding" contract="Wcf.IService1">  
  3.           <identity>  
  4.             <dns value="localhost" />  
  5.           </identity>  
  6.         </endpoint>  
  7.         <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />  
  8. </service>  
  9.   
  10. <behavior name="Wcf.Service1Behavior">  
  11.           <serviceMetadata httpGetEnabled="true" />  
  12.           <serviceDebug includeExceptionDetailInFaults="false" />  
  13. </behavior>  
xii. Now Save the project and Build the application.
 

Host WCF service in SharePoint 2010

 
I. Start Internet Information Services (IIS) Manager. Click Start => All Programs => Administrative Tools => Internet Information Services (IIS) Manager.
 
ii. In Internet Information Services (IIS) Manager, right-click on Sites, and select Add Web Site.
 
image3.gif 
 
iii. In the Add Web Site dialog box, in the Site name field, enter WCF. In the Physical path field, enter C:\Users\kfcb554pa.tst\Desktop\Project\Wcf\Wcf. In the Port field, enter 6000 and click OK. 
 
image4.gif 
 
image5.gif 
 
iv. Validate that the web service is running. Start Internet Explorer, and browse to http://ukcbemtsekikm01:6000/CustomService.svc. (Note: ukcbemtsekikm01 is the server name). If the web service is running, you will see the following:
 
image6.gif 
 
v. Another approach to test the Web service is to use the WCF test client. Start a Visual Studio command prompt. Enter wcftestclient to run the WCF test client.
 
image7.gif 
 
vi. Click File => Add Service.
 
image8.gif 
 
vii. Enter http://ukcbemtsekikm01:6000/CustomService.svc as the endpoint address, and click OK.
 
image9.gif 
 
viii. If the service was added successfully, you will see the methods that the service exposes as shown below.
 
image10.gif 
 
ix. Double-click on GetTime. This opens a window that allows you to configure the request and invoke the request.
 
x. Thus the WCF has been created and hosted inside SharePoint 2010.


Similar Articles