Consuming WCF Data Service

The purpose of this tutorial is to show you how to make a call to an ADO.NET Data Service in the context of a Silverlight application. The following common tasks will be examined:

  • Adding a reference.

  • Creating a new instance of the exposed entity.

  • Making asynchronous call to the service and consuming the service result.

The process of developing an ADO.NET Data Service is beyond the scope of this tutorial. For more information read here.

This tutorial will use the following product and technology:
* Northwind database, which can be downloaded from here.
* ADO.NET Entity Framework - used to create an Entity Data Model representation of the Northwind database.

The Telerik OpenAccess ORM also supports excellent integration with ADO.NET Data Services.

Adding Reference

The first step is to add a new service reference to your application. In order to do that you need to perform the following steps:

  • Select the "References" folder, right click with your mouse button and choose "Add Service Reference..." Common Consuming Data Ado Net Data Service 010

  • A popup window appears, hit the Discover button to find the web service or enter the service location. Press OK to add it. Common Consuming Data Ado Net Data Service 020

You have the ability to choose the type of the collections that the service returns. In order to do that you need to open the Service Reference Settings dialog from the Advanced button. Common Consuming Data Ado Net Data Service 030

When a service reference is added to a project, any types defined in the service are generated in the local project. In many cases, this creates duplicate types when a service uses common .NET Framework types or when types are defined in a shared library. To avoid this problem, types in referenced assemblies are shared by default. If you want to disable type sharing for one or more assemblies, you can do so in the Service Reference Settings dialog.

Once the ADO.NET data service is added all needed assemblies will be included in your project automatically.

Creating New Instance of the Exposed Entity

NorthwindEntities northwindEntity = new NorthwindEntities( new Uri( "http://localhost:54248/Services/SampleAdoNetDataService.svc/" ) ); 
Dim northwindEntity As New NorthwindEntities(New Uri("http://localhost:54248/Services/SampleAdoNetDataService.svc/")) 
NorthwindEntities represents the runtime context of a given data service. While data services themselves are stateless, the context is not, so state on the client is maintained between interactions in order to support features such as identity resolution and optimistic concurrency.

Making Asynchronous Call to the Service and Consuming the Result

In Silverlight all service calls are performed asynchronously. In order to make an asynchronous call to your service you need to do the following steps:

  • Create a new DataServiceQuery.

  • Execute the DataServiceQuery asynchronously.

Here is a sample code showing how this can be achieved:

public static void BeginLoadingProducts( Categories category ) 
    DataServiceQuery<Products> categoryProducts = northwindEntity 
        .CreateQuery<Products>( string.Format( "Categories({0})/Products", category.CategoryID ) ) 
        .Expand( "Suppliers" ) 
        .Expand( "Categories" ); 
        ( IAsyncResult result ) => EntitiesLoaded<Products>( result, category.Products ), 
        categoryProducts ); 
private static void EntitiesLoaded<T>( IAsyncResult result, Collection<T> entities ) 
    DataServiceQuery<T> query = result.AsyncState as DataServiceQuery<T>; 
    foreach ( T entity in query.EndExecute( result ) ) 
        entities.Add( entity ); 
Public Shared Sub BeginLoadingProducts(ByVal category As Categories) 
    Dim categoryProducts As DataServiceQuery(Of Products) = northwindEntity.CreateQuery(Of Products)(String.Format("Categories({0})/Products", category.CategoryID)).Expand("Suppliers").Expand("Categories") 
    categoryProducts.BeginExecute(Function(ByVal result As IAsyncResult) EntitiesLoaded(Of Products)(result, category.Products), categoryProducts) 
End Sub 
Private Shared Sub EntitiesLoaded(Of T)(ByVal result As IAsyncResult, ByVal entities As Collection(Of T)) 
    Dim query As DataServiceQuery(Of T) = TryCast(result.AsyncState, DataServiceQuery(Of T)) 
    For Each entity As T In query.EndExecute(result) 
End Sub 

Practically all the work about the consuming the result is done in the EntitesLoaded method.

In this article
Not finding the help you need? Improve this article