In this lab we will see how to consume the JavaScript Object Notation (JSON) response from WCF service in Silverlight client application. This is the response type that can be consumed in Microsoft .NET as well as third-party client applications to develop real-world line of business (LOB) applications.
Exercise 1: Creating WCF JSON Service.
Task 1: Open VS2010 and create a blank solution, name it as 'SILV4_ConsumingJSON_WCF_Service'.
Task 2: In this solution, add a new WCF Service application and 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: Right-click on 'Service.svc' and select 'View Markup' and make the following changes:
<%@ ServiceHost="" Language="C#" Debug="true" Service="WCF_JSON_Service.Service" CodeBehind="Service.svc.cs" %>
Task 4: Open 'IService.cs' and write the following code:
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
namespace WCF_JSON_Service
{
[ServiceContract]
public interface IService
{
[OperationContract]
[WebGet(UriTemplate = "Employee",
BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
List<Employee> GetAllEmployee();
}
[DataContract]
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; }
}
}
In the code above, the method "GetAllEmployee" returns an object of "List<Employee>". This method is also attributed with the [WebMethod] attribute class. This class is present under the namespace "System.ServiceModel.Web". This namespace is designed for "JSON" and "REST" based WCF service creation. The "RequestFormat" and "ResposeFormat" is set to "JSON". This returns the JSON data. The "UriTemplate" property replaces the name of the method in the URL. See the following example.
The URL will be:
http://localhost:13272/Service.svc/Employee
Task 5: Open "Service.svc.cs" and implement the "IService" interface in the "Service" class as below:
using System.Collections.Generic;
namespace WCF_JSON_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},
};
}
}
}
Write the Web.Config file as below:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="WCF_JSON_Service.Service" behaviorConfiguration="ServBehave">
<endpoint
address=""
binding="webHttpBinding"
behaviorConfiguration="edpBehavior" contract="WCF_JSON_Service.IService"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServBehave">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="edpBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
The endpoint behavior "<webHttp/>" is specified for the JSON Response from the WCF Service.
Task 6: Publish this service on IIS (recommended). Now browse the svc file in the browser, when you press Enter, the "Employee" file will be downloaded as below:
Click on "Save"; after giving the path, the file will be saved. Now open the file in Notepad, the response will be as below:
This completes the creation of the WCF JSON Response service.