Consume WCF REST Services using WCF REST Kit and JSON communication (using Windows Forms client)

by Jagadish Pulakhandam on 3/2/2012 7:34:09 AM
Rated 0 from 0 votes
Brief: Demonstrates consuming a WCF REST service using a simple Windows Forms Client Application. Uses JSON for communication/data exchange and WCF REST Kit as REST consuming library.
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 fetch data from WCF REST service using Windows Forms (using HTTP GET operations) with JSON communication
  • How to use HttpClient class available in WCF REST Kit
  • How to work with the HttpResponseMessage object (which is a result from HttpClient.Get operation)
  • How to convert or deserialize JSON returned from a WCF REST service to a .NET custom object.
  • How to bind the results to DataGridView (Windows Forms)
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.

1. Retrieving single row 

Screen shot:



Source Code (in VB.NET)

01.Imports System.Net
02.Imports System.IO
03.Imports System.Text
04.Imports Microsoft.Http
05.Imports SampleEmpServiceLib
06. 
07.Public Class Form1
08. 
09.    Private Sub btnGetEmployeeDetails_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetEmployeeDetails.Click
10.        Dim oClient As New HttpClient(New Uri("http://localhost:8080/SampleEmpService/"))
11.        Dim resp As HttpResponseMessage = oClient.Get("Employees/" & txtEmpno.Text)
12.        resp.EnsureStatusIsSuccessful()
13.        Dim oEmp As Employee = System.Runtime.Serialization.Json.JsonContentExtensions.ReadAsJsonDataContract(Of Employee)(resp.Content)
14. 
15.        Me.lblEname.Text = oEmp.Ename
16.        Me.lblSal.Text = oEmp.Sal
17.        Me.lblDeptno.Text = oEmp.Deptno
18.    End Sub
19. 
20. 
21.End Class

2. Retrieving multiple rows of data (and binding to DataGridView)

Screen shot:



Source Code (in VB.NET):

01.Imports System.Net
02.Imports System.IO
03.Imports System.Text
04.Imports Microsoft.Http
05.Imports SampleEmpServiceLib
06. 
07.Public Class Form1
08. 
09.    Private Sub btnShowEmployees_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowEmployees.Click
10.        Dim oClient As New HttpClient(New Uri("http://localhost:8080/SampleEmpService/"))
11.        Dim resp As HttpResponseMessage = oClient.Get("Employees")
12.        resp.EnsureStatusIsSuccessful()
13.        Dim lstEmp As List(Of Employee) = System.Runtime.Serialization.Json.JsonContentExtensions.ReadAsJsonDataContract(Of List(Of Employee))(resp.Content)
14.        Me.DataGridView1.DataSource = lstEmp
15.    End Sub
16. 
17. 
18.End Class


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


Discussion
No comments yet!