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.
- Open Visual Studio 2010.
- Go to File => New => Project.
- Select Empty SharePoint Project under installed template category SharePoint and name it WCF.
- Target the .Net Framework 3.5.
- Click OK.
- 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".
- Right-click the Solution Explorer, click Add and select the SharePoint Mapped Folder.
- In Add SharePoint Mapped Folder select ISAPI.
- Right-click on ISAPI and Add a New Folder.
- Name the folder WCF.
- Right-click on WCF and Add a New item.
- Select a text file from the installed templates and name it Service.svc.
- Add another text file and name it web.config.
- Right-click on the ISAPI and Add a new item.
- Select a Class file from the installed templates and name it Service.cs.
- Again add a new item, select Interface file and name it IService.cs.
- The solution in the Solution Explorer should look like the following:
- 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:
- Go to Run and type inetmgr.
- Expand sites and click on SharePoint- 5000 (SharePoint Site where we are going to deploy the WCF service).
- Click Authentication as shown in the above figure.
- Check whether the status of Anonymous Authentication is enabled.
Testing the Service:
- Deploy the Project.
- Go to the URL http://demo2010a:5000/_vti_bin/WCF/Service.svc.
- We have successfully created a WCF service and hosted it inside a SharePoint (_vti_bin folder).