Perform an UPDATE/DELETE operations using WCF REST Services (with JSON Communication)

by Jagadish Pulakhandam on 2/28/2012 11:24:51 AM
Rated 0 from 0 votes
Brief: Demonstrates updating/modifying/deleting data through a WCF REST service using a simple Windows Forms Client Application. Uses JSON for communication/data exchange.
Posted to: Consuming WCF REST Services
Add to DiggAdd to del.icio.usAdd to FURLAdd to RedditAdd to YahooAdd to BlinklistAdd to GoogleAdd to ma.gnoliaAdd to ShadowsAdd to Technorati

The sample demonstrates the following:
  • How to update/delete data from database using WCF REST service using Windows Forms (using HTTP PUT and DELETE operations) with JSON communication
  • How to use WebRequest class to post to WCF REST service
  • How to use HttpWebRequest and HttpWebResponse to work with WCF REST service
  • Working 'Stream' object returned by HttpWebResponse
  • How to convert or serialize a .NET object to JSON in order to work with WCF REST service
  • How to use DataContractJsonSerializer (available in System.Runtime.Serialization.Json namespace)
NOTE: The samples use JSON to communicate to WCF REST service.  If you need source code of WCF REST Service or XML based communication with WCF REST service, check out "Related Posts" section.

Screen shot:



Source code (in VB.NET):

01.Imports System.Net
02.Imports System.IO
03.Imports System.Text
04.Imports System.Runtime.Serialization.Json
05. 
06.Public Class Form1
07. 
08.    Dim uri As String = "http://localhost:8080/SampleEmpService/Employees"
09. 
10.    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
11.        Dim req As HttpWebRequest = WebRequest.Create(uri & "/" & Me.txtEmpno.Text)
12.        Dim resp As HttpWebResponse = req.GetResponse()
13.        Dim respStream As Stream = resp.GetResponseStream()
14. 
15.        'Dim readStream As New StreamReader(respStream, Encoding.UTF8)
16.        'MessageBox.Show(readStream.ReadToEnd())
17. 
18.        Dim ser As New DataContractJsonSerializer(GetType(Employee))
19.        Dim oEmp As Employee = CType(ser.ReadObject(respStream), Employee)
20.        Me.txtEname.Text = oEmp.Ename
21.        Me.txtSal.Text = oEmp.Sal
22.        Me.txtDeptno.Text = oEmp.Deptno
23. 
24.        respStream.Close()
25.        resp.Close()
26.    End Sub
27. 
28.    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
29. 
30.        Dim oEmp As New Employee With {
31.            .Empno = txtEmpno.Text,
32.            .Ename = txtEname.Text,
33.            .Sal = txtSal.Text,
34.            .Deptno = txtDeptno.Text
35.            }
36. 
37.        Dim ser As New DataContractJsonSerializer(GetType(Employee))
38.        Dim ms As New MemoryStream
39.        ser.WriteObject(ms, oEmp)
40. 
41.        'Dim jsonData As String = Encoding.UTF8.GetString(ms.ToArray)
42.        'MessageBox.Show(jsonData)
43. 
44.        Dim arr() As Byte = ms.ToArray
45.        Dim req As HttpWebRequest = WebRequest.Create(uri)
46.        req.Method = "PUT"
47.        req.ContentType = "application/json"
48.        req.ContentLength = arr.Length
49. 
50.        Dim reqStrm As Stream = req.GetRequestStream()
51.        reqStrm.Write(arr, 0, arr.Length)
52.        reqStrm.Close()
53. 
54.        Dim resp As HttpWebResponse = req.GetResponse()
55.        MessageBox.Show(resp.StatusDescription)
56. 
57.        ''only if necessary
58.        'Dim respStream As Stream = resp.GetResponseStream
59.        'Dim sr As New StreamReader(respStream)
60.        'MessageBox.Show(sr.ReadToEnd)
61.        'sr.Close()
62. 
63.        reqStrm.Close()
64.        resp.Close()
65.    End Sub
66. 
67.    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
68. 
69.        Dim req As HttpWebRequest = WebRequest.Create(uri & "/" & Me.txtEmpno.Text)
70.        req.Method = "DELETE"
71.        Dim resp As HttpWebResponse = req.GetResponse()
72.        MessageBox.Show(resp.StatusDescription)
73. 
74.        ''only if necessary
75.        'Dim respStream As Stream = resp.GetResponseStream
76.        'Dim sr As New StreamReader(respStream)
77.        'MessageBox.Show(sr.ReadToEnd)
78.        'sr.Close()
79. 
80.        resp.Close()
81.    End Sub
82. 
83.    Public Class Employee
84.        Public Empno As String
85.        Public Ename As String
86.        Public Sal As String
87.        Public Deptno As String
88.    End Class
89. 
90.End Class

Join the .NET Code Central Community and join the discussion!
Signing-up is FREE and quick. Do it now, we want to hear your opinion
0

Rated 0 from 0 votes ( login  to rate)
DotnetKicks DotnetKicksDe DotNetShoutout

Attachments / Source Code
You need to Login or Join for FREE to download the following