Performing DML Operations using WCF REST Service and Silverlight

Abstract: WCF REST has provided a mechanism for developing loosely coupled applications. Using Silverlight 3.0 as a client, a light weight consumer of WCF REST service can be created without adding a proxy into the client application.

Performing DML Operations using WCF REST Service and Silverlight

As a professional working on WCF REST and Silverlight 3.0, I have been very interested in performing DML operations using Silverlight 3.0 and WCF REST Service which exposes CRUD Operations. A couple of days ago, while conducting a training session, one of the participants asked me a question about WCF REST Services exposing CRUD operations and consuming it using Silverlight 3.0, for loosely coupled application development. I immediately thought of doing some research on it and convert it into an article to help out other users who may have the same requirement.

Now most of you might have started working on WCF REST - one of the most important features of WCF. It allows using WCF service by clients like PHP, JAVA etc. REST supports verbs like GET, PUT, POST and DELETE. These verbs represent operations performed using http requests.

In this article I have designed a WCF REST service which is performing GET and POST operations and it is used by Silverlight 3.0 client application. Let us see the steps required to implement the solution.

Step 1: Open VS2008, create a blank solution and name it as ‘SILV3_CallingRESTDMLService’. To this solution, add a new WCF Service Application form ‘Web’ project Type and name it as ‘WCF_DMLService’ as shown below:

Step 2: Rename ‘IService1.cs’ to ‘IService’ and ‘Service1.Svc.cs’ to ‘Service.Svc’.

Step 3: Since this is a WCF REST service, add a reference to ‘System.ServiceModel.Web’. This namespace provides classes for defining behavior for Operations in Service for REST. Web Service Host activation for the WCF service is provided by this namespace.

Step 4: Right Click on the ‘Service.Svc’ and select ‘View Markup’ and change the file as shown below:

In the above code, I have used ‘WebGet’ and ‘WebInvoke’ attributes along with ‘OperationContract’. These attributes are present under the namespace ‘System.ServiceModel.Web’ and indicates that operations defined in the contract will be called by the web programming model. WebGet has HTTP ‘GET’ by default and ‘WebInvoke’ can be used for HTTP ‘PUT’, ‘POST’ and ‘DELETE’ verbs. Request and Response formats can be either ‘XML’ or ‘JSON’.

Currently it shows all the employees. One important thing here is that you can perform this operation only for ‘WebGet’ and not for ‘WebInvoke’ because the browser can perform only HTTP GET operation. Close the browser.

Step 10: To the same solution, add a new Silverlight application and name it as ‘SILV3_RESTDMLClient’.

Step 11: Design the Xaml UI as per your requirements. I am not a very good designer so pardon me for the design shown below J

Since all calls from Silverlight to Web resources are asynchronous, I have used classes like:

WebClient - used for downloading all contents asynchronously and

WebRequest and WebResponse - for asynchronous ‘POST’ operations.

Step 13: To the Silverlight project, add a reference to ‘System.Runtime.Serialization’. Note that this reference must be targeting to version ‘2.0.5.0’. Silverlight 3.0 does not support the framework like 3.0/3.5 for this reference. (If any knows any workaround, I am all ears)

In the above code, I have made use of XLINQ. Using ‘WebClient’ class the request is made to the url for downloading string contents form the web. Since WCF REST used POX, the response is in the form of XML. This XML data is read using XLINQ and the data is put in Employee class.

The above code makes use of ‘WebRequest’ and ‘WebResponse’ classes and its asynchronous methods like:

·BeginGetRequestStream

·EndGetRequestStream - Returns data to be written to the internet resource.

·BeginGetResponse

·EndGetResponse - Returns the data from the internet.

The above ‘End’ methods indicate completion of Request and Response Operations to Web resources over internet. Remember without completing this Async, operation calls will not be completed.

Step 14: Now run the Silverlight application and click on ‘Get All’. You will get all the rows from the table displayed in a DataGrid. Similarly you can also test the Insert and Delete operations.

Enter Data in text boxes and press on ‘Insert’ button and then click on the ‘Get All’ button to see the newly added record. The newly added record is shown below (marked in RED).

The same way you can test ‘Delete’ too.

Conclusion: WCF REST has provided a mechanism for developing loosely coupled applications. Using Silverlight 3.0 as a client, a light weight consumer of WCF REST service can be created without adding a proxy into the client application.

Mahesh Sabnis is a Microsoft MVP having 14 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). Follow him on twitter @maheshdotnet

Feedback - Leave us some adulation, criticism and everything in between!