calling wcf service from silverlight

2 posts, 0 answers
  1. cielo valdoz
    cielo valdoz avatar
    33 posts
    Member since:
    Dec 2009

    Posted 25 Mar 2010 Link to this post


    I created a WCF web service to get data from sql database but im not sure how to call the web service from Silverlight.
    I search for articles but no luck. I have to return the Top Sales Item from SQL database then generate a telerik chart. I declared an instance of WCF Service, but it doesnt go to the  mService_GetTop10SalesItemsCompleted event. Kindly check the code below.

    Web Service
    Imports System.ServiceModel 
    Imports System.ServiceModel.Activation 
    ' NOTE: If you change the class name "IEGSDashboardService" here, you must also update the reference to "IEGSDashboardService" in Web.config. 
    <ServiceContract()> _ 
    Public Interface IEGSDashboardService 
        <OperationContract()> _ 
         Function GetTop10SalesItems(ByVal intLocation As IntegerByVal dteDay As DateAs List(Of TopSalesItem) 
    End Interface 

    ' NOTE: If you change the class name "EGSDashboardService" here, you must also update the reference to "EGSDashboardService" in Web.config and in the associated .svc file. 
    Public Class EGSDashboardService 
        Implements IEGSDashboardService 
        Public Function GetTop10SalesItems(ByVal intLocation As IntegerByVal dteDay As DateAs System.Collections.Generic.List(Of TopSalesItem) Implements IEGSDashboardService.GetTop10SalesItems 
            If intLocation = 0 Then intLocation = -1 
            Dim SalesItemList As New List(Of TopSalesItem) 
                Dim ctx As New DashboardDataContext 
                Dim q = From c In ctx.DashboardGetTopSalesItems(intLocation, dteDay, dteDay.AddDays(30)) 
                For Each item In q 
                    SalesItemList.Add(New TopSalesItem(item.Name, item.Amount)) 
            Catch ex As Exception 
                SalesItemList = Nothing 
            End Try 
            Return SalesItemList 
        End Function 

    Public Class TopSalesItem 
        Private _name As String 
        Private _amount As Double 
        Public Property Name() As String 
                Return _name 
            End Get 
            Set(ByVal value As String
                _name = value 
            End Set 
        End Property 
        Public Property Amount() As Double 
                Return _amount 
            End Get 
            Set(ByVal value As Double
                _amount = value 
            End Set 
        End Property 
        Public Sub New(ByVal name As StringByVal amount As Double
            _name = name 
            _amount = amount 
        End Sub 
        Public Sub New() 
        End Sub 
    End Class 

    Imports Telerik.Windows.Controls.Charting 
    Imports System.Xml.Linq 
    Imports System.Windows.Interop 
    Imports System.Windows.Resources 
    Imports System.Reflection 
    Imports System.IO 
    Imports System.ComponentModel 
    Imports System.Linq 
    Imports System.Collections.ObjectModel 
    Imports System.Collections.Generic 
    Imports System.Net 
    Imports Telerik.Windows.Controls 
    Imports EGSDashboard_SL.DashboardService 
    Partial Public Class MainPage 
        Inherits UserControl 
        Public intLoc As Integer 
        Public strLoc As String 
        Public intDrillDown As Integer 
        'Create a new instance of our WCF Service 
        Public WithEvents mService As New EGSDashboardServiceClient 
        Public Sub New() 
        End Sub 
        Private Sub MainPage_Loaded(ByVal sender As ObjectByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded 
                Dim mService As DashboardService.EGSDashboardServiceClient = New DashboardService.EGSDashboardServiceClient() 
                AddHandler mService.GetTop10SalesItemsCompleted, AddressOf mService_GetTop10SalesItemsCompleted 
                mService.GetTop10SalesItemsAsync(-1, "03/23/2010"
        End Sub 
    Private Sub mService_GetTop10SalesItemsCompleted(ByVal sender As ObjectByVal e As DashboardService.GetTop10SalesItemsCompletedEventArgs) Handles mService.GetTop10SalesItemsCompleted 
            Dim x As Object 
            x = e.Result 
        End Sub 


  2. Tihomir Petkov
    Tihomir Petkov avatar
    576 posts

    Posted 29 Mar 2010 Link to this post

    Hello Cielo,

    Please watch the following video that explains how to consume web services in Silverlight and let me know if you still have difficulties after watching it. I will be glad to help:

    Best wishes,
    Tihomir Petkov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Back to Top