This is a migrated thread and some comments may be shown as answers.

calling wcf service from silverlight

1 Answer 66 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
cielo valdoz
Top achievements
Rank 1
cielo valdoz asked on 26 Mar 2010, 04:04 AM
Hi,

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) 
 
        Try 
            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)) 
            Next 
        Catch ex As Exception 
            SalesItemList = Nothing 
        End Try 
        Return SalesItemList 
    End Function 

Class
Public Class TopSalesItem 
    Private _name As String 
    Private _amount As Double 
 
    Public Property Name() As String 
 
        Get 
            Return _name 
        End Get 
 
        Set(ByVal value As String
            _name = value 
        End Set 
    End Property 
 
    Public Property Amount() As Double 
        Get 
            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 
 


XAML
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() 
        InitializeComponent() 
    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 

Thanks,
Cielo


1 Answer, 1 is accepted

Sort by
0
Tihomir Petkov
Telerik team
answered on 29 Mar 2010, 04:26 PM
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:

http://www.silverlight.net/learn/videos/all/how-to-consume-wcf-and-aspnet-web-services-in-silverlight/

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.
Tags
General Discussions
Asked by
cielo valdoz
Top achievements
Rank 1
Answers by
Tihomir Petkov
Telerik team
Share this question
or