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.
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.
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.
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.
Host WCF service in SharePoint 2010
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.
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.
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:
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.
vi. Click File => Add Service.
vii. Enter http://ukcbemtsekikm01:6000/CustomService.svc as the endpoint
address, and click OK.
viii. If the service was added successfully, you will see the methods that the
service exposes as shown below.
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.