New to Telerik UI for WPF? Download free 30-day trial

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 WPF application. The following common tasks will be examined:

You can check our RadDataServiceDataSource which provides seamless integration between an user interface and data coming from a WCF Data Service.

  • Adding a reference.

  • Creating a new instance of the exposed entity.

  • Making 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 Northwind database, which can be downloaded from http://services.odata.org/Northwind/Northwind.svc

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

Adding Reference

The first step is to add a new connected service to your application. In order to add the needed connected service, you will need to add the OData Connected Service Visual Studio extension. After adding this extension you can follow the steps below.

The OData Connected Service tool works only with Visual Studio 2017.

Figure 1: Select the "References" folder, right click with your mouse button and choose "Add Connected Service"

Common Consuming Data Wcf Service 010

Figure 2: A new tab will be opened with the available conntected services. Click on the OData Connected Service.

Common Consuming Data Wcf Service 020

Figure 3: A popup window will appear. In the Address TextBox, type the Northwind URL address and click on the Finish button below.

Common Consuming Data Wcf Service 030

When a service reference is added to a project, any types defined in the service are generated in the local project.

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

Creating New Instance of the exposed Entities

Example 1: Create new instance of the NorthwindEntities

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
        InitializeComponent(); 
        NorthwindEntities serviceClient = new NorthwindEntities(new Uri("http://services.odata.org/Northwind/Northwind.svc")); 
    } 
} 
Public Partial Class MainWindow 
    Inherits Window 
 
    Public Sub New() 
        InitializeComponent() 
        Dim serviceClient As NorthwindEntities = New NorthwindEntities(New Uri("http://services.odata.org/Northwind/Northwind.svc")) 
    End Sub 
End Class 

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

Making a call to an ADO.NET Data Service is a simple process. Here is a sample code, demonstrating how to load all categories from the Categories table in the Northwind database:

public partial class MainWindow : Window 
{ 
    public ObservableCollection<Category> Categories { get; set; } 
    public MainWindow() 
    { 
        InitializeComponent(); 
        NorthwindEntities serviceClient = new NorthwindEntities(new Uri("http://services.odata.org/Northwind/Northwind.svc"));               
        foreach (Category c in serviceClient.Categories.Execute()) 
        { 
            this.Categories.Add(c); 
        } 
    } 
} 
Public Partial Class MainWindow 
    Inherits Window 
 
    Public Property Categories As ObservableCollection(Of Category) 
 
    Public Sub New() 
        InitializeComponent() 
        Dim serviceClient As NorthwindEntities = New NorthwindEntities(New Uri("http://services.odata.org/Northwind/Northwind.svc")) 
 
        For Each c As Category In serviceClient.Categories.Execute() 
            Me.Categories.Add(c) 
        Next 
    End Sub 
End Class 
In this article