How to Create and Host WCF Service Inside SharePoint 2010

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 a SharePoint site (in the _vti_bin folder).
 

Procedure to create a WCF Service

 
The following procedure should be followed to create a WCF service using Visual Studio 2010.
  1. Open Visual Studio 2010.
  2. Go to File => New => Project.

    1.gif

  3. Select Empty SharePoint Project under installed template category SharePoint and name it WCF.
  4. Target the .Net Framework 3.5.
  5. Click OK.
  6. In the SharePoint Customization Wizard, enter the URL of the local site where you want to use (http://demo2010a:5000/) and select "Deploy as a farm solution".
  7. Right-click the Solution Explorer, click Add and select the SharePoint Mapped Folder.

    2.gif

  8. In Add SharePoint Mapped Folder select ISAPI.

    3.gif

  9. Right-click on ISAPI and Add a New Folder.

    4.gif

  10. Name the folder WCF.
  11. Right-click on WCF and Add a New item.
  12. Select a text file from the installed templates and name it Service.svc.

    5.gif

  13. Add another text file and name it web.config.

    6.gif

  14. Right-click on the ISAPI and Add a new item.
  15. Select a Class file from the installed templates and name it Service.cs.

    7.gif

  16. Again add a new item, select Interface file and name it IService.cs.

    8.gif

  17. The solution in the Solution Explorer should look like the following:

    9.gif

  18. Add a reference to the Microsoft.SharePoint.Client.ServerRuntime assembly. This assembly contains the Service Host Factory classes, as well as some attributes we need later on.
IService.cs
  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. namespace WCF.ISAPI    
  8. {    
  9.     [ServiceContract]    
  10.     public interface IService    
  11.     {    
  12.         [OperationContract]    
  13.         string getTime();    
  14.     }    
  15. } 
Service.cs
  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. using System.ServiceModel.Activation;  
  8. namespace WCF.ISAPI  
  9. {  
  10.     [ServiceBehavior]  
  11.     [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]  
  12.      public class Service : IService  
  13.     {  
  14.          public string getTime()  
  15.        {  
  16.               returnDateTime.Now.ToString();  
  17.        }  
  18.     }    
  19. }  
Service.svc
 
To get Version, PublicKeyToken, and Assembly type name use the SN.EXE utitlity. 
  1. <%@ServiceHostDebug="true" Language="C#" CodeBehind="Service.cs"   
  2. Service="WCF.ISAPI.Service,WCF, PublicKeyToken=1b762f52dfc6b6d8,Version=1.0.0.0, Culture=Neutral"%>   
web.config
  1. <?xml version="1.0"?>  
  2. <configuration>  
  3.   <system.serviceModel>  
  4.     <services>  
  5.       <service behaviorConfiguration="WCF.ISAPI.ServiceBehaviour"  
  6.         name="WCF.ISAPI.Service">  
  7.         <endpoint address=""  
  8.                   binding="basicHttpBinding"  
  9.                   contract="WCF.ISAPI.Service" >  
  10.           <identity>  
  11.             <dns value="localhost" />  
  12.           </identity>  
  13.         </endpoint>  
  14.         <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />  
  15.         <host>  
  16.           <baseAddresses>  
  17.             <add baseAddress="http://demo2010a:5000/"></add>  
  18.           </baseAddresses>  
  19.         </host>  
  20.       </service>  
  21.     </services>  
  22.     <behaviors>  
  23.       <serviceBehaviors>  
  24.         <behavior name=" WCF.ISAPI.ServiceBehaviour">  
  25.           <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->  
  26.           <serviceMetadata httpGetEnabled="true"/>  
  27.           <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid  
  28. disclosing exception information -->  
  29.           <serviceDebug includeExceptionDetailInFaults="false"/>  
  30.         </behavior>  
  31.       </serviceBehaviors>  
  32.     </behaviors>  
  33.   </system.serviceModel>  
  34. </configuration>  

Checking anonymous authentication to the SharePoint site

  1. Go to Run and type inetmgr.

    10.gif

  2. Expand sites and click on SharePoint- 5000 (SharePoint Site where we are going to deploy the WCF service).

    11.gif

  3. Click Authentication as shown in the above figure.

    12.gif

  4. Check whether the status of Anonymous Authentication is enabled.

Testing the WCF Service

  1. Deploy the Project.
  2. Go to the URL http://demo2010a:5000/_vti_bin/WCF/Service.svc.

    13.gif

  3. We have successfully created a WCF service and hosted it inside a SharePoint (_vti_bin folder).


Similar Articles