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

The DataServiceContext

RadDataServiceDataSource requires a DataServiceContext and a DataServiceQuery<T> in order to connect to a remote server.

The DataServiceContext

A class that derives from the DataServiceContext class will be automatically generated for you when you add the Service Reference to your project. It can be located in the generated code-behind file of your Service Reference. You might need to select the Show All Files option in Solution Explorer in order to see the code-behind file.

WPF RadDataServiceDataSource Generated Files WPF

A typical auto-generated DataServiceContext class will look like this:

public partial class NorthwindEntities : global::System.Data.Services.Client.DataServiceContext 
Partial Public Class NorthwindEntities Inherits Global.System.Data.Services.Client.DataServiceContext 

Setting DataServiceContext in code-behind

In order to create an instance of the DataServiceContext class, you need to specify the WCF Data Service Uri in its constructor. The simply assign to the respective property on RadDataServiceDataSource:

Uri serviceUri = new Uri("", UriKind.Absolute); 
NorthwindEntities northwindContext = new NorthwindEntities(serviceUri); 
this.radDataServiceDataSource.DataServiceContext = northwindContext; 
Dim serviceUri As New Uri("", UriKind.Absolute) 
Dim northwindContext As New NorthwindEntities(serviceUri) 
Me.radDataServiceDataSource.DataServiceContext = northwindContextSetting DataServiceContext in XAML 

Setting DataServiceContext in XAML

Since the DataServiceContext class does not have a parameterless constructor, it cannot be directly instantiated in XAML. This can be worked around by creating another class with a parameterless constructor that derives from it and then hard-coding the Uri in the call to the base constructor:

public class MyNorthwindContext : NorthwindEntities 
    public MyNorthwindContext() : base(new Uri("", UriKind.Absolute)){} 
Public Class MyNorthwindContext 
 Inherits NorthwindEntities 
 Public Sub New() 
  MyBase.New(New Uri("", UriKind.Absolute)) 
 End Sub 
End Class 

Once you have a context class with a parameterless constructor, you can easily instantiate it in XAML:

<telerik:RadDataServiceDataSource Name="customersDataSource" QueryName="Customers" AutoLoad="True"> 

Once you assign the DataServiceContext to the control, do not set any of its properties or call any of its methods. RadDataServiceDataSource needs to be the only entity that operates with the context. WCF Data Services_ are stateless, but the DataServiceContext is not. Tampering with the DataServiceContext of a RadDataServiceDataSource may lead to unexpected results. In case you need to perform unrelated tasks, you can always create another context and work with it, instead of modifying the one that is currently in use by RadDataServiceDataSource.

The only DataServiceContext.MergeOption that is supported is MergeOption.OverwriteChanges. Setting the DataServiceContext.MergeOption to anything else will result in an error.

In this article