Silverlight 4: Consuming WCF REST Service using JSON

A nice feature of WCF programming is the support for REST and JSON, using which a client can communicate with the WCF service without using A proxy. In this article, I have used a Silverlight 4 client application to make a call to WCF service using JSON. In Silverlight, we have been provided with the System.Json assembly. This provides a ‘JsonArray’ class which represents an ordered sequence of the JSON value objects represented by the JsonValue object. The Load method provided by this class deserializes the result into CLR objects which can then be used for the further processing in the client application.

Step 1: Open VS2010 and create a blank solution, name it as ‘SL4_Calling_REST_Service_with_JSON’. In this solution, add a new WCF service (targeted to .NET 4.0) and call this project as ‘WCF_REST_Service’. Define the following ServiceContract, OperationContract and DataContract in the IService.cs file.

WCF Service

Note that the GetEmployees() method defines JSON as the request and response format from both sides.

Step 2: Implement the above interface in the Service class as shown below in Service.svc.cs file:

WCF Service Class

Step 3: Define the Web.Config file as shown below with the addition of protocol mapping for WebHttpBinding and endpoint behavior for WebHttp.

Web http Binding

Step 4: Publish the service on the IIS Web Server.

Note: On the Root of the IIS, you must have the clientaccesspolicy.xml file so that Silverlight client can make call to it.

Step 5: In the same solution, add a new Silverlight application and call it ‘SL4_JSON_Client’. Add a reference to System.Json assembly in the application. In MainPage.xaml, add a Button and DataGrid control as shown below and also subscribe to the Click event of the Button :

Silverlight Button DataGrid

Step 6: In the MainPage.Xaml.cs, add the Employee class as shown below. This class is used to store the result of the query fired on the result, from the JSON response.


Step 7: Add the following code on the Click event of the ‘Get Employees’ button. This code makes an async call to the WCF REST Service using OpenReadAsync method of the WebClient class, by passing WCF Service URL to it. The OpenReadCompleted event will collect the JSON response from the service and then this result will be processed. The code is as below:


Note: Read the comments carefully.

Step 8: Run the application, the result will be as shown below:
Download the source code

About The Author

Mahesh Sabnis is a Microsoft MVP having over 18 years of experience in IT education and development. He is a Microsoft Certified Trainer (MCT) since 2005 and has conducted various Corporate Training programs for .NET Technologies (all versions). He also blogs regularly at Follow him on twitter @maheshdotnet

1 comment:

Anonymous said...

Good words!
a few comments:
1) I had to change the Target Framework to "Microsoft Entity Framework June 2011 CTP" to get a reference to System.ServiceModel.Web for the WebGet attribute. This was not easy nor apparent.
2) I think when you start the project, instead, create a WCF Service Library, call it WCF_REST_Service, and call the solution "SL_Calling_REST_Service_with_JSON" to get the same/better results.