Windows Scripting
  Home arrow Windows Scripting arrow Page 3 - Creating a Silverlight 2.0 Application tha...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

Creating a Silverlight 2.0 Application that Consumes a WCF Service
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2008-11-05

    Table of Contents:
  • Creating a Silverlight 2.0 Application that Consumes a WCF Service
  • Configuring for Silverlight
  • Writing Code
  • Adding a Project

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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):


    More Windows Scripting Articles
    More By Jagadish Chaterjee


       · Hello guys,This is my second article in series focusing on "Siverlight 2.0...
       · This is the most thorough Silverlight data binding I found so far. However I hot...
     

    WINDOWS SCRIPTING ARTICLES

    - More Windows Scripting Workarounds from Nilpo
    - Overloading Methods and More in VBScript
    - Improving MFC for Windows Vista
    - Regular Expressions in VBScript
    - Working with Dates in WMI
    - Completing Calendars with VBScript Date Func...
    - Building Calendars with VBScript Date Functi...
    - Working With Dates and Times in VBScript
    - Designing WCF DataContract Classes Using the...
    - Understanding Dates and Times in VBScript
    - Working With Arrays in VBScript
    - Compressed Folders in WSH
    - Using .NET Interops in VBScript
    - Nilpo`s Scripting Secrets, Vol I
    - Database operations using Silverlight 2.0 WC...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek