Silverlight Consuming WCF REST Service

In this lab we will see how to consume a WCF REST service in Silverlight client applications. Represential State Transfer (REST) is provided in WCF to respond from the WCF service in Plain Old XML (POX) format. This is the new feature in WCF 3.5 SP1. Using this feature, any client application that understands HTTP calls and XML parsing can consume the WCF service. It is not necessary for the client application to consume the WCF service.
 
Exercise 1: Creating WCF Service
 
Task  1:  Open VS2008/VS2010 and create a blank solution, name it:
 
"SILV_Consuming_WCF_REST_Service".
 
Task 2: In this solution, add a new WCF Service application, name it:
 
"WCF_JSON_Service". Rename "IService1.cs" to "IService.cs", this will rename the interface "IService1" to "IService". Rename "Service1.svc" to "Service.svc". Open Service.svc and rename "Service1" class to "Service".
 
Task 3: To create the WCF REST service, the "System.ServiceModel.Web" namespace must be present in the WCF project. (Note: This is already present in the references.) Click on "Service.svc" and select "View Markup" and make the following changes:

<%@ ServiceHost="" Language="C#" Debug="true" Service="WCF_REST_Service.Service" CodeBehind="Service.svc.cs" Factory="System.ServiceModel.Activation.WebServiceHostFactory"%>

Note

The Factory attribute ensure that the "System.ServiceModel.Activation.WebServiceHostFactory" class is used to host the WCF service as a web style web service that generates a response in XML format (POX).
 
Task 4: Open "IService.cs" and create the following "ServiceContract", "OpenrationContract" and "DataContract" as below:

using System.Collections.Generic;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.ServiceModel.Web;

 

namespace WCF_REST_Service

{

    [ServiceContract]

    public interface IService

    {

        [OperationContract]

        [WebGet(UriTemplate = "Employee",

BodyStyle = WebMessageBodyStyle.Bare,

RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]

        List<Employee> GetAllEmployee();

    }

    [DataContract(Name = "Employee", Namespace = "")]

    public class Employee

    {

        [DataMember]

        public int EmpNo { get; set; }

        [DataMember]

        public string EmpName { get; set; }

        [DataMember]

        public string DeptName { get; set; }

        [DataMember]

        public int Salary { get; set; }

    }

}

Note that the "WebGet" attribute defines "RequestFormat" and "ResponseFormat" as "Xml". The "UriTemplate" property is used to write the URL as below:
 
http://localhost:8001/WCF_REST_HP_VD/Service.svc/Employee
 
Task 5: Open Service.svc and implement the "IService" interface in the "Service" class as below:
 

using System.Collections.Generic;

 

namespace WCF_REST_Service

{

    public class Service : IService

    {

        public List<Employee> GetAllEmployee()

        {

            return new List<Employee>()

{

new Employee(){EmpNo=101,EmpName="Ajay", DeptName="Depe_1",Salary=45000},

new Employee(){EmpNo=102,EmpName="Sankay", DeptName="Depe_1",Salary=55000},

new Employee(){EmpNo=103,EmpName="Vijay", DeptName="Depe_2",Salary=15000},

new Employee(){EmpNo=104,EmpName="Makarand", DeptName="Depe_2",Salary=25000},

new Employee(){EmpNo=105,EmpName="Amol", DeptName="Depe_3",Salary=45000},

new Employee(){EmpNo=106,EmpName="Rajesh", DeptName="Depe_3",Salary=55000},

new Employee(){EmpNo=107,EmpName="Vinit", DeptName="Depe_4",Salary=75000},

new Employee(){EmpNo=108,EmpName="Vinod", DeptName="Depe_4",Salary=65000},

new Employee(){EmpNo=109,EmpName="Kaushal", DeptName="Depe_5",Salary=85000},

new Employee(){EmpNo=110,EmpName="Vikas", DeptName="Depe_5",Salary=95000},

};

        }

    }

}
 
The Web.Config file is as below:
 

<system.serviceModel>

  <services>

    <service behaviorConfiguration="ServBehave" name="WCF_REST_Service.Service">

      <endpoint address="" binding="webHttpBinding" contract="WCF_REST_Service.IService">

      </endpoint>

    </service>

  </services>

  <behaviors>

    <serviceBehaviors>

      <behavior name="ServBehave">

        <serviceMetadata httpGetEnabled="true"/>

        <serviceDebug includeExceptionDetailInFaults="false"/>

      </behavior>

    </serviceBehaviors>

  </behaviors>

</system.serviceModel>
 
Task 6: Publish the service on IIS (recommended). Now browse the "Service.svc" file; the following result will be displayed:

Silverlight-Consuming-WCF-REST-Service-1.jpg
 
Note that "Endpoint not found" is the result that is displayed, this means that the service is successfully hosted. Now change the URL as below:
 
http://localhost:8001/WCF_REST_HP_VD/Service.svc/Employee
 
Press "Enter"; the following result will be displayed.
 
The preceding is the result from the WCF service.

Silverlight-Consuming-WCF-REST-Service-2.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all