Hosting WCF Service inside IIS

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.



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.

  1. using System;

  2. using System.Collections.Generic;

  3. using System.Linq;

  4. using System.Runtime.Serialization;

  5. using System.ServiceModel;

  6. using System.Text; 

  7. namespace Wcf

  8. {

  9.     [ServiceContract]

  10. public interface ICustomService

  11.     {

  12.       [OperationContract]

  13. string GetTime();

  14.     }

  15. }

x. Replace CustomerService.svc.cs with the following code.

  1. using System;

  2. using System.Collections.Generic;

  3. using System.Linq;

  4. using System.Runtime.Serialization;

  5. using System.ServiceModel;

  6. using System.Text;

  7. namespace Wcf

  8. {

  9. public class CustomService : ICustomService

  10.     {

  11. public string GetTime()

  12.         {

  13. return DateTime.Now.ToString();

  14.         }

  15.     }

  16. }

xi. In the Web.config file remove the below code.

  1. <service behaviorConfiguration="Wcf.Service1Behavior" name="Wcf.Service1">

  2.         <endpoint address="" binding="basicHttpBinding" contract="Wcf.IService1">

  3.            <identity>

  4.             <dns value="localhost" />

  5.           </identity>

  6.         </endpoint>

  7.         <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />

  8.       </service>

  9. <behavior name="Wcf.Service1Behavior">

  10.           <serviceMetadata httpGetEnabled="true" />

  11.           <serviceDebug includeExceptionDetailInFaults="false" />

  12.         </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.








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.