Silverlight 3 - Part 3 (Data Services)

Posted by in Silverlight category on for Beginner level | Views : 9461 red flag
Rating: 5 out of 5  
 1 vote(s)

This Article will help to start using Services in Silverlight 3.0. We will see how we do Data transfer operations using ADO.NET Data Services. Please refer to my previous articles on Sliverlight Part1, Part2 to get started

Silverlight is one of the fast growing platforms for rich client application. But to show data on client side it needs to connect to a data source and in this Service Model will help to perform different operations related to data on database. Below diagram illustrates this Model.


This is just a web service but with additional options to expose ADO.NET Entity Models related to functionality over a service to perform CRUD (Create, Read, Update and Delete) over HTTP using RESTful-style web services. Clients can use LINQ instead of manually using WebClient or HttpWebRequest to consume the service. It provides all these operations by implementing IQueryable and IUpdatable to perform CRUD options. It also provides other features like Authorization, Business Logic and Custom Service Operations. Below diagram illustrates about ADO.NET Data Services as mentioned above.

Client can consume this Service by adding the Service as reference and use LINQ to perform CRUD operations in an easy way. Below example uses Silverlight as client to consume this data service.
Silverlight Demo Application

Below example shows how we can create a Data Service using Visual Studio 2008.
Follow the same steps as in Article Silverlight 3 – Part 1. Then follow the below steps.
Select Entity Data Model as below


And select a database to generate a model. We will use a Northwind sample database as below.


Select all the tables and create Entity Model.


Now create a Data Service as below


Add SampleService.svc and add the below code to code behind

Now we can add the service to Silverlight project.


To show data in silverlight will just add DataGrid to silverlight MainPage.xaml and code behind as below.

Add data grid in XAML
Add the below code in code behind. In silverlight that data retrival method should execute in Async Mode only otherwise you will get Exceptions.

ServiceReference1.NorthwindEntities myEnt = null;
        public MainPage()
            myEnt = new SilverlightwithService.ServiceReference1.NorthwindEntities(new Uri(HtmlPage.Document.DocumentUri,"SampleService.svc"));
            var dsQuery = (DataServiceQuery<Products>)(from p in myEnt.Products select p);
            dsQuery.BeginExecute(result => test.DataContext = dsQuery.EndExecute(result).ToList(), null);

And if you execute the page will look like below. You see all the products in the Grid using Data Service.


ADO.NET Data Service provides all types of data related operations that you want from Silverlight Side. All CURD operations can be executed from LINQ directly and the data transfer between client and server can happen in XML or JSON depending on the settings.

Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Sudhakar Kottapalli
Member Level: Bronze
Member Status: Member
Member Since: 10/5/2009 7:05:50 AM

Login to vote for this post.

Comments or Responses

Posted by: Deeraj on: 3/19/2010
Hi Sudhakar,

Firstly, Thanks for the article.

Did you try hosting it in IIS and were you able to get the data without any problem?

I used IIS5.1 on Win XP With SP2.


Login to post response

Comment using Facebook(Author doesn't get notification)