Reading Data from WCF Data Service in Silverlight 4

Posted by: Mahesh Sabnis , on 6/13/2011, in Category Windows Communication Foundation (WCF)
Views: 77890
Abstract: WCF data Services provides integration with Data Model built using ADO.NET EF. Since the data received is in the form of XML, any client application built on Open standards can easily consume it. In this article, I will demonstrate how it is done using Silverlight 4.

As part of .NET Framework 4.0 and Windows Communication Foundation (WCF) 4.0, ADO.NET Entity Framework integration is provided using WCF Data Services. The WCF Data Services exposes entity data (provided by ADO.NET EF) as a data service.

WCF Data Services enables the creation and consumption of OData services for the web. Note that WCF Data Service was formerly known as ADO.NET Data Services.

To design a WCF Data Service, we need to do the following:

Design the Data Model using ADO.NET EF:

  • This is done with the help of ADO.NET Entity Framework. This is based upon Object Relational Mapping (O-R-M) where we get the CLR objects mapped with the database table.
  • In this case, the query can be fired directly on the mapped objects which returns an instance of IQueryable interface.
  • Using this mapping, it is possible for us to perform Create, Read, Update and Delete operations on the Database.

Create a Data Service:

  • Here the DataService<T> class is used as a base for exposing the Data Service. It contains the entry point for the entity container which is defined by the Data Model.

Configure Access Rules for the Data Service:

  • This is the most important step. Here you need to configure the Data Service which defines AccessRules for entities defined in the Data Model.
  • Based upon the operation you want to perform, the access rule must be specified.
  • DataServiceConfiguration class is used for Data Service configuration and setting Access Rules.

In this article demonstration, I have used Sql Server 2008 and the Database is ‘Company’ with the following table:

WCF Data Service Table

Step 1: Open VS2010 and create a Blank solution, name it as ‘WCF_DataService_And_Silverlight_4’

Step 2: In this solution, add a new WCF Service application and call it ‘WCF_DataService’. Delete IService1.cs and Service1.svc from the project, as we are going to make a use of the WCF Data Service Template.

Step 3: Right-click the project and add a new ADO.NET Entity Data Model, name it as ‘CompanyEDMX’. Follow the wizard, select ‘Company’ as database and select Department table. The EDMX file should get added in the project as shown below:

entity-framework-edmx

Step 4: Right-click on the project, name it as ‘WCF_DataService_Company.svc’ and add the new WCF Data Service in the project. Make the following changes in it:

wcf-data-services-company

Step 5: Build the project and make sure that it is error free.

Step 6: Right-Click on WCF_DataService_Company.svc and view in browser, you should see the following:

WCF Data Service Browser

The Url will be ‘http://localhost:<some Port>/WCF_DataService_Company.svc/

Step 7: Now change the Url to the one shown below:

‘http://localhost:<some port>/WCF_DataService_Company.svc/Departments’

you will find all rows from Department table in the form of an Xml document as below:

WCF XML format

If for some reason, you do not see xml and instead you are getting a Feed, then make the following changes from Internet Options (in IE):

clip_image006

Step 7: In the same solution, add a new Silverlight application and name it as ‘SL4_Client_WCF_Data-Service’. Make sure that you will host it in the same WCF Service project created above by keeping the defaults shown in the following window:

WCF Data Service Project

This will add ‘SL4_Client_WCF_Data-ServiceTestPage.aspx’ and ‘SL4_Client_WCF_Data-ServiceTestPage.html’ in the ‘WCF_DataService’ project.

Step 8: In the Silverlight project, add the WCF service reference and name the service namespace as MyRef.

Step 9: Open MainPage.xaml and drag drop a button and DataGrid element in it:

wcf data service consume

Step 10: In the MainPage.Xaml.cs use the following namespaces:

  • using System.Data.Services.Client;
  • using System.Windows.Data;
  • using SL4_Client_WCF_Data_Service.MyRef;

Step 11: In the MainPage class level, declare the following objects:

  • MyRef.CompanyEntities objContext;
  • DataServiceCollection<Department> deptDataCollection;

In .NET 4.0, the DataServiceCollection<T> class is introduced in WCF Data Services to support databinding with the controls in a client application. This class is especially used in WPF and Silverlight.

Step 12: In the UserControl_Loaded event, write the following code for creating an instance of the objectContext proxy for the WCF data service using the Service uri and an instance of DataServiceCollection:

wcf dataservice collection

Step 13: In the ‘Get Data’ button click event, make an async call to the WCF Data Service by firing the query as shown below:

wcf async data call

The query is executed asynchronously and the result is generated which is further assigned to the ItemsSource property of the DataGrid.

Step 14: Run the application and click on Get Data button. The following output will be displayed:

WCF Data Service Demo

Conclusion: WCF data Services provides integration with Data Model built using ADO.NET EF. Since the data received is in the form of XML, any client application built on Open standards can easily consume it.

The entire source code of this article can be download over here

Note: Here the aspx/html text page is in the same WCF service project, the WCF Service is currently hosted on the local port and not on IIS.

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
Mahesh Sabnis is a DotNetCurry author and a Microsoft MVP having over two decades of experience in IT education and development. He is a Microsoft Certified Trainer (MCT) since 2005 and has conducted various Corporate Training programs for .NET Technologies (all versions), and Front-end technologies like Angular and React. Follow him on twitter @maheshdotnet or connect with him on LinkedIn


Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by MarkusR on Monday, June 20, 2011 5:41 PM
Hello,

I am getting a cross-domain error with the downloaded project. How do I fix?
Comment posted by Masood Ali on Tuesday, March 6, 2012 10:51 AM
hi,

The sample which is provided is different than  in this article. Im getting an error on this

custCollection.LoadCompleted += new EventHandler<LoadCompletedEventArgs>(custCollection.LoadCompleted);  
Comment posted by Ali on Tuesday, March 6, 2012 1:01 PM
my bad. works perfectly