Observable Collections
page 5 of 9
by frans eilering
Feedback
Average Rating: 
Views (Total / Last 10 Days): 38777/ 117

ServiceReference

A ServiceReference will be used as link between the Silverlight part and the web part. It can be seen as a webservice. You ask a question and the result comes back. Asynchronously. This means "Don't wait for the answer. If it is there you should handle it".

First Build the project. It's a good practice to regularly build it.

First, create the Service reference.

Right click the web part.

Select "New item"

 

Select WCF Service and click Add.

A new WCF Service reference will be created with the name Service1.

Two files have been added to the web project.

Service1.svc.vb and IService1.vb.

The are closely linked. A function inserted in Service1.svc.vb  must be inserted as an OperationContract in IService1.vb

New code for IService1.vb

Imports System.ServiceModel
 
' NOTE: You can use the "Rename" command on the context menu to change the 
' interface name "IService1" in both code and config file together.
<ServiceContract()>
Public Interface IService1
 
    <OperationContract()>
    Sub DoWork()
 
    <OperationContract()> _
    Function FindContactList() As List(Of demo.Contact)
 
End Interface

You may remove the sub DoWork. The IService1.vb can contain more OperationContracts.

In Service1.svc.vb insert the code below.

New code for Service1.svc.vb

Imports System.Data.SqlClient
 
' NOTE: You can use the "Rename" command on the context menu to change the class 
' name "Service1" in code, svc and config file together.
Public Class Service1
    Implements IService1
 
    Public Sub DoWork() Implements IService1.DoWork
    End Sub
 
    Public Function FindContactList() As List(Of demo.Contact) Implements 
IService1.FindContactList
        Return New ContactManager().FindContactList()
    End Function
 
 
    Public Class ContactManager
        Public Function FindContactList() As List(Of demo.Contact)
            Dim OContact As demo.Contact
            OContact = New demo.Contact
 
            Dim Bdr As System.Data.SqlClient.SqlDataReader  ' IS CLOSED BELOW F
            Bdr = OContact.GetsContactAll
 
            Dim ContactList = New List(Of demo.Contact)()
            If Bdr IsNot Nothing Then
                Do While Bdr.Read()
                    Dim NewContact = CreateNewContact(Bdr)
                    ContactList.Add(NewContact)
                Loop
            End If
            Bdr = Nothing
 
            Return ContactList
        End Function
 
        Private Function CreateNewContact(ByVal rdr As SqlDataReader) As 
demo.Contact
            Dim NewContact = New demo.Contact With {
                .IDContact = rdr("IDContact").ToString(),
                .Name = rdr("Name").ToString(),
                .Address = rdr("Address").ToString(),
                .City = rdr("City").ToString()
            }
            Return NewContact
        End Function
    End Class
End Class
 

If you call the function FindContactList then a new object OContact is created. Then through our SQLHelper a SQLDataReader is filled with the information coming from the GetsContactAll function in our Contact Class. Then iterate through our SQLDataReader to create a list.

 

Again, build the solution.

Add the service reference

Right click your silverlight part of the project and select "Add Service reference"

Click the "Discover" button to look for Services in your project.

And click OK.

It will take some time but then you will see a new Servicereference1.

This will be the file to communicate with from your Silverlight part.


View Entire Article

User Comments

Title: C# Code example   
Name: eilering
Date: 2011-09-16 3:50:25 AM
Comment:
The VB code is very basic. If you copy the code and use a convertor like
http://converter.telerik.com/
then you can paste it and you get the C# code.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-10-08 8:18:33 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search