Creating a Silverlight 2.0 Application that Consumes a WCF Service - Writing Code
(Page 3 of 4 )
Once you have created and configured your WCF Service as explained in previous sections, you need to develop some code to retrieve rows from the database.
Open “IEmpService.vb” and modify the code so that it looks like the following:
<ServiceContract()> _
Public Interface IEmpService
<OperationContract()> _
Function GetEmployee(ByVal empno As Integer) As Employee
<OperationContract()> _
Function GetEmployeeList() As List(Of Employee)
End Interface
' Use a data contract as illustrated in the sample below to add composite types to service operations.
<DataContract()> _
Public Class Employee
Private _Empno As Integer
Private _Ename As String
Private _Sal As Double
Private _Deptno As Integer
<DataMember()> _
Public Property Empno() As Integer
Get
Return _Empno
End Get
Set(ByVal value As Integer)
_Empno = value
End Set
End Property
<DataMember()> _
Public Property Ename() As String
Get
Return _Ename
End Get
Set(ByVal value As String)
_Ename = value
End Set
End Property
<DataMember()> _
Public Property Sal() As Double
Get
Return _Sal
End Get
Set(ByVal value As Double)
_Sal = value
End Set
End Property
<DataMember()> _
Public Property Deptno() As Integer
Get
Return _Deptno
End Get
Set(ByVal value As Integer)
_Deptno = value
End Set
End Property
End Class
Open “EmpService.svc” and modify the code as follows:
Imports System.Data.SqlClient
Public Class EmpService
Implements IEmpService
Public Sub New()
End Sub
Public Function GetEmployee(ByVal empno As Integer) As Employee Implements IEmpService.GetEmployee
Dim objEmp As Employee = Nothing
Using cmd As New SqlCommand("select empno, ename, sal, deptno from dbo.emp where empno = " & empno, New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("cnSample").ConnectionString))
cmd.Connection.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader
If dr.HasRows Then
dr.Read()
objEmp = New Employee With {.Empno = dr.GetValue(dr.GetOrdinal("Empno")), _
.Ename = dr.GetValue(dr.GetOrdinal("Ename")), _
.Sal = dr.GetValue(dr.GetOrdinal("Sal")), _
.Deptno = dr.GetValue(dr.GetOrdinal("Deptno")) _
}
End If
cmd.Connection.Close()
End Using
Return objEmp
End Function
Public Function GetEmployeeList() As System.Collections.Generic.List(Of Employee) Implements IEmpService.GetEmployeeList
Dim clnEmp As New List(Of Employee)
Using cmd As New SqlCommand("select empno, ename, sal, deptno from dbo.emp ", New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("cnSample").ConnectionString))
cmd.Connection.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader
If dr.HasRows Then
While dr.Read
clnEmp.Add(New Employee With {.Empno = dr.GetValue(dr.GetOrdinal("Empno")), _
.Ename = dr.GetValue(dr.GetOrdinal("Ename")), _
.Sal = dr.GetValue(dr.GetOrdinal("Sal")), _
.Deptno = dr.GetValue(dr.GetOrdinal("Deptno")) _
} _
)
End While
End If
cmd.Connection.Close()
End Using
Return clnEmp
End Function
End Class
Build your solution and open “EmpService.svc” in a browser. It should respond with the following (Fig 06).

Developing WCF Service using Visual Studio 2008: Testing WCF Service
The easiest and fastest way to test a WCF Service is by using the “WCF Test Client” which comes with Visual Studio 2008. The other way is to develop our own WCF consumer (say WinForm consumer, Unit Test consumer) and write code to access the WCF Service. We will deal with the first method here (for simplicity). The second method (WinForm consumer) is available in the source download.
WCF Service test using “WCF Test Client”:
Open Start || Programs || Visual Studio 2008 || Visual Studio Tools || Visual Studio 2008 Command Prompt.
At the prompt, type “wcftestclient” and press enter.
You should see “WCF Test Client” opened for you.
Go to File || Add service, provide “http://localhost/DemoEmpService/EmpService.svc” as the endpoint address and click “OK.”
It should connect to your WCF service and show you the list of web methods.
Double click on those methods and start testing as follows (Fig 07):

Next: Adding a Project >>
More Windows Scripting Articles
More By Jagadish Chaterjee