Search This Blog

Tuesday, February 2, 2016

How to Create and Host WCF Service Inside SharePoint 2013

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:
using System;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Runtime.Serialization;usingSystem.ServiceModel;usingSystem.Text;namespace WCF.ISAPI{    [ServiceContract]    publicinterfaceIService    {        [OperationContract]        stringgetTime();    }}
Service.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;
namespace WCF.ISAPI
{
    [ServiceBehavior]
    [AspNetCompatibilityRequirements(RequirementsMode =AspNetCompatibilityRequirementsMode.Allowed)]
     public class Service : IService    {
         public string getTime()
       {
              returnDateTime.Now.ToString();
       }
    }  
}

Service.svc:
To get Version, PublicKeyToken, and Assembly type name use the SN.EXE utitlity. 

<%@ServiceHostDebug="true"Language="C#"CodeBehind="Service.cs"Service="WCF.ISAPI.Service,WCF, PublicKeyToken=1b762f52dfc6b6d8,Version=1.0.0.0, Culture=Neutral"%>

web.config:
<?xml version="1.0"?>
<configuration>
  <system.serviceModel>
    <services>
      <service behaviorConfiguration="WCF.ISAPI.ServiceBehaviour"
        name="WCF.ISAPI.Service">
        <endpoint address=""
                  binding="basicHttpBinding"
                  contract="WCF.ISAPI.Service" >
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding"contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://demo2010a:5000/"></add>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name=" WCF.ISAPI.ServiceBehaviour">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid
disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</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 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).

No comments:

Post a Comment