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 .
Steps Involved:
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.
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Runtime.Serialization;
using
System.ServiceModel;
using
System.Text;
namespace
Wcf
{
[ServiceContract]
public interface
ICustomService
{
[OperationContract]
string GetTime();
}
}
x. Replace CustomerService.svc.cs with the following code.
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Runtime.Serialization;
using
System.ServiceModel;
using
System.Text;
namespace
Wcf
{
public
class CustomService
: ICustomService
{
public
string GetTime()
{
return
DateTime.Now.ToString();
}
}
}
xi. In the Web.config file remove
the below code.
<service
behaviorConfiguration="Wcf.Service1Behavior"
name="Wcf.Service1">
<endpoint
address=""
binding="basicHttpBinding"
contract="Wcf.IService1">
<identity>
<dns
value="localhost"
/>
</identity>
</endpoint>
<endpoint
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange"
/>
</service>
<behavior
name="Wcf.Service1Behavior">
<serviceMetadata
httpGetEnabled="true"
/>
<serviceDebug
includeExceptionDetailInFaults="false"
/>
</behavior>
xii. Now Save the project and Build the application.
Add a new Web Site:
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.