Sunday, March 28, 2010

Hosting WCF Service Inside SharePoint 2010

In SharePoint 2010  Out of the box all the Lists are exposed as WCF Restful Service using OData Protocol. Here is path or URL to access SharePoint Lists WCF Restful Services .   http://ServerName/_vti_bin/ListData.svc. The following screenshot shows the collections of List from my SharePoint site.

image

In this blog post  we are going to see how to host Custom WCF service Inside SharePoint 2010 and we will consume the same service from Silverlight client . Up front we know that we are going to call this WCF service from Silverlight so i am going to host this service in basicHttpBinding.

First  create a empty SharePoint project.

image

Now we need to provide the site where you wanted to host this WCF service and how we are going to deploy it either SandBox or Server Farm.  Here  i am going to choose Farm solution.

image

Once the project is successfully created. Now we need to Add  SharePoint ISAPI Mapping folder. To do this right clicking the project and choose the SharePoint Mapped Folder as shown below.

image

Adding /Mapping SharePoint  ISAPI Folder.

image

Now add C# Interface file and Class File to define the WCF Service Contract and Service implementation.  and also we need to add .svc and web.config file to host the service.

image

Simple WCF Service Contract:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Microsoft.SharePoint;

namespace WCFSVCINSHAREPOINT
{
[ServiceContract]
public interface ICustomService
{
[OperationContract]
string GetTime();
}
}


Service Implementation 



using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Microsoft.SharePoint;
using System.ServiceModel.Activation;

namespace WCFSVCINSHAREPOINT
{
[ServiceBehavior]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class CustomService : ICustomService
{
public string GetTime()
{
return DateTime.Now.ToString();
}

}
}


.SVC file



image



You can use .NET Reflector to get the following ServiceHost attributes PublicKeyToken , Assembly Type name, Version. But i  used the   Strong name  (SN.EXE ) Utility to get all the above attributes . Here is the sample how to use SN.EXE



image



Web.Config file



image



Now Fire up the IE and Browse the WCF Service. [Note: Make sure that your SharePoint Site has Anonymous access as  Shown below.]



image



image



Next Blog post we will see how to call this Service from Silverlight Client.



Nandri(Thanks)



SreenivasaRagavan

6 comments:

Meyer Denney said...

Great post! Perhaps in the future you could make that web.config file not an image so we can copy and paste it easier? :) Keep up the good work.

Shoeb said...

for mexHttpBinding, where does the baseaddress comes from in the web.config file ?

peterccli said...

Question :

How did sharepoint 44119 get defined ?

HOw is your wcf getting added to this location ?

Thanks, Peter

Viel Rebsardan said...

WOW! Its awesome.. I really appreciate this post. I hope to see more post from you sooner.

dearnemo said...
This comment has been removed by the author.
dearnemo said...

Hi,
How did you come up with in baseAddressweb.config ? Please let me know. Thanks