Objective
- How to create REST service for JSON request and response format.
- How to create SilverLight Client, which will consume a REST service on JSON data format.
- How to POST data in JSON format from SilverLight client.
Working explanation
Here, I am trying to insert data in static list and fetch it back at SilverLight client.
Step 1
Create new WCF Service Application
File -> New -> Project -> Web -> WCF Service Application
Step 2
Remove all the default code in service1.svc and IService1.cs. Paste the below code there in IService1.cs .
Note
- If you have changed the name of service interface then take care of that. If you have changed IService1 to ITest then update code in ITest.cs. if you have changed name of Service1.cs then update code in changed class.
- In this sample, I am adding object of Test class in a static list and fetching it back. If you want to perform pure Data base operation (CRUD). Just replace Insert code and get Code with your database operation code.
- Add reference of System.ServiceModel.Web to IService1.cs.
-
Now what is Test class here? And where it should be declared and created. So just right click on service project and add a class. Give name of the class Test and paste below code over there.
Test.cs
namespace SilverLightRestTesting { public class Test{ public int Marks { get; set; } public String Name { get; set; }}} |
IService1.cs
using System;
using System.Collections.Generic;using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; using System.ServiceModel.Web; namespace SilverLightRestTesting { [ServiceContract] public interface IService1{ [OperationContract] |
Service1.cs
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace SilverLightRestTesting |
Step 3
Open Web.Config file and delete below highlighted code. In other words delete all default bindings and endpoints for existing service model. Delete existing <system.serviceModel>
Step 4
Create a SilverLight application.
- Create two buttons on SilverLight markup. One for getting the records from the service and other for Insert into the service. .
-
Right click on SilverLight application and add the class Test. Because in REST services DataContract are not exposed to the client.
Test.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace SilverLightRestTesting |
- Let us suppose button name is Insert for Insert into Static list of service. Write below code on click event of Insert button.
private void Insert_Click(object sender, RoutedEventArgs e){
Test t1 = new Test() { Name = “Civics”, Marks = 100 }; cnt.UploadStringCompleted += new UploadStringCompletedEventHandler(cnt_UploadStringCompleted); |
- To get all the records in form of JSON from REST service , let us suppose button name is Display. Then write below code on click event of Display button.
private void display_Click(object sender, RoutedEventArgs e) WebClient cnt = new WebClient(); cnt.DownloadStringCompleted += new DownloadStringCompletedEventHandler(cnt_DownloadStringCompleted); json = newJsonArray { JsonObject.Parse(str) asJsonObject }; json = JsonArray.Parse(str) asJsonArray; |
Leave a Reply