Data Access has been discontinued. Please refer to this page for more information.

Walkthrough: Creating a WCF Data Services Solution

This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.

In this walkthrough, you will create a WCF Data Services application that retrieves data from the SofiaCarRental sample database. You will use Silverlight to create the client in the presentation tier. You will create entity classes that represent various database tables. This walkthrough serves as a starting point for other walkthroughs.

Creating a Silverlight Solution

  1. In Visual Studio, create a new Silverlight project by selecting File, New, and then Project. The New Project dialog box appears.
  2. Select the Silverlight Application template and name the new project OA.SL.WcfData.Demo.

  3. Click OK. The New Silverlight Application dialog box appears.

  4. Click OK to create the solution. The solution contains two projects: a client project and a server project. The client project is named OA.SL.WcfData.Demo and it contains the Silverlight code that you use to create the presentation tier. The server project is named OA.SL.WcfData.Demo.Web and it contains the middle-tier code.

Creating Telerik Data Access Domain Model

In this section, you will create, a Telerik Data Access Domain Model that represents data from the SofiaCarRental sample database.

To make data available in the middle tier

  1. In Solution Explorer, right-click the server project (OA.SL.WcfData.Demo.Web), select Add, and then select New Item. The Add New Item dialog box appears.
  2. In the list of categories, select Data and then select the Telerik Data Access Domain Model item.
  3. Name the new file SofiaCarRentalDomainModel.rlinq and click Add. The Telerik Data Access Create Model Wizard appears.
  4. In the Select Domain Model Type screen, select Populate from database. Enter SofiaCarRentalDbContext for model name.
  5. In the Setup Database Connection screen, create a data connection to the SofiaCarRental database and click Next.
  6. In the Choose Database Items screen, select the all tables.

  7. Click Finish. Persistent classes are generated for the tables.

  8. Build the solution.

Creating a WCF Data Service

In this task is demonstrated how to create a WCF Data Service by using the Service Wizard, how to test the service in a web browser, what files are generated by the wizard.

To create a new WCF Data Service

  1. Build your solution.
  2. Right-click the SofiaCarRentalDomainModel.rlinq and select Add Telerik Data Access Service....

  3. This will bring up the Service Wizard. The first page of the wizard is the Select Source and Output dialog. From the Select context drop-down select SofiaCarRentalDbContext. From the Host in Project drop-down select OA.SL.WcfData.Demo.Web. Check the Use Existing Project option and select OA.SL.WcfData.Demo.Web.

  4. Click Next to continue. The Select Service Type dialog appears. Select WCF Data Service v3.

  5. Click Next to continue. In the Service Configuration dialog, set the Service Name to SofiaCarRentalWCFDataService. Select which entities from the grid below to be generated as part of the service. By default all entities are selected. Also you have the option to specify what CUD operations can be performed on the generated entities. Check all entities.

  6. Click Next to continue. In the Preview dialog, review all changes that will be done in your project.

  7. Click Finish to generate the service.

  8. The Service Wizard performs the following steps:

    1. Sets references to all of the WCF Data Services libraries.
    2. Sets references to the Telerik Data Access DLLs.
    3. And last, the wizard creates the actual service files (.svc and .cs).

      [System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults = true)]
      public partial class SofiaCarRentalWCFDataService :
         OpenAccessDataService<OA.SL.WcfData.Demo.Web.SofiaCarRentalDbContext>
      {
          /// <summary>
          /// Initializes the service.
          /// </summary>
          /// <param name="config">The configuration object.</param>
          public static void InitializeService(DataServiceConfiguration config)
          {
              config.SetEntitySetAccessRule("RentalRates", EntitySetRights.All);
              config.SetEntitySetAccessRule("RentalOrders", EntitySetRights.All);
              config.SetEntitySetAccessRule("Employees", EntitySetRights.All);
              config.SetEntitySetAccessRule("Customers", EntitySetRights.All);
              config.SetEntitySetAccessRule("Categories", EntitySetRights.All);
              config.SetEntitySetAccessRule("Cars", EntitySetRights.All);
              // TODO: Set service behavior configuration options
              // Examples:
              // config.DataServiceBehavior.AcceptCountRequests = true;
              // config.DataServiceBehavior.AcceptProjectionRequests = true;
              config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
          }
      }
      
      <System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults:=True)> _
      Partial Public Class SofiaCarRentalWCFDataService
          Inherits OpenAccessDataService(Of OA.SL.WcfData.Demo.Web.SofiaCarRentalDbContext)
          Public Shared Sub InitializeService(ByVal config As DataServiceConfiguration)
              config.SetEntitySetAccessRule("RentalRates", EntitySetRights.All)
              config.SetEntitySetAccessRule("RentalOrders", EntitySetRights.All)
              config.SetEntitySetAccessRule("Employees", EntitySetRights.All)
              config.SetEntitySetAccessRule("Customers", EntitySetRights.All)
              config.SetEntitySetAccessRule("Categories", EntitySetRights.All)
              config.SetEntitySetAccessRule("Cars", EntitySetRights.All)
              ' TODO: Set service behavior configuration options
              ' Examples:
              ' config.DataServiceBehavior.AcceptCountRequests = true
              ' config.DataServiceBehavior.AcceptProjectionRequests = true
              config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3
          End Sub
      End Class
      

Adding Reference to the WCF Data Service

To add a WCF Data Service reference to the project:

  1. Right-click the SilverlightClient project, click Add Service Reference, and then click Discover. This displays the SofiaCarRentalWCFDataService that you have created in the previous task.
  2. In the Namespace text box, type SofiaCarRentalWcfDataService, and then click OK.
  3. This adds to the project a new code file that contains the data classes which are used to access and interact with data service resources as objects. The data classes are defined in the default namespace of the client application. These data classes are created by using the Telerik Data Access Model Tools.

Creating Silverlight Client Application

For this walkthrough, you will use the generated WCF Data Service.

To display the data in the Silverlight client

  1. Open MainPage.xaml.
  2. From the Toolbox, drag a DataGrid control to within the Grid element in XAML view. Name the DataGrid CustomersGrid as shown in the following XAML.

    <UserControl x:Class="OA.SL.WcfData.Demo.MainPage"
       xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
       xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
       <Grid x:Name="LayoutRoot" Background="White">
           <sdk:DataGrid Name="CustomersGrid"/>
       </Grid>
    </UserControl>
    
  3. Open the code-behind for MainPage.xaml.

  4. Add code to instantiate the SofiaCarRentalDomainContext, retrieve customers by calling the GetCustomersQuery method, and bind the results to the DataGrid, as shown in the following code.

    using System;
    using System.Collections.ObjectModel;
    using System.Data.Services.Client;
    using System.Windows.Controls;
    using OA.SL.WcfData.Demo.SofiaCarRentalWcfDataService;
    namespace OA.SL.WcfData.Demo
    {
       public partial class MainPage : UserControl
       {
           private SofiaCarRentalWcfDataService.SofiaCarRentalDbContext dataManager = 
               new SofiaCarRentalWcfDataService.SofiaCarRentalDbContext( 
                   new Uri( "SofiaCarRentalWCFDataService.svc", UriKind.Relative ) );
           public MainPage()
           {
               InitializeComponent();
               DataServiceQuery<Customer> query = dataManager.Customers;
               query.BeginExecute(
                   s =>
                   {
                       DataServiceQuery<Customer> state = s.AsyncState as DataServiceQuery<Customer>;
                       ObservableCollection<Customer> data = new ObservableCollection<Customer>();
                       foreach ( var entity in state.EndExecute( s ) )
                           data.Add( entity );
                       this.CustomersGrid.ItemsSource = data;
                   }, query );
           }
       }
    }
    
    Imports System.Data.Services.Client
    Imports OA.SL.WcfData.Demo.SofiaCarRentalWCFDataService
    Imports System.Collections.ObjectModel
    Partial Public Class MainPage
        Inherits UserControl
        Private dataManager As New SofiaCarRentalWCFDataService.
            SofiaCarRentalDbContext(New Uri("SofiaCarRentalWCFDataService.svc", UriKind.Relative))
        Public Sub New()
            InitializeComponent()
            Dim query As DataServiceQuery(Of Customer) = dataManager.Customers
            query.BeginExecute(Sub(s)
               Dim state As DataServiceQuery(Of Customer) = _
                   TryCast(s.AsyncState, DataServiceQuery(Of Customer))
               Dim data As New ObservableCollection(Of Customer)()
               For Each entity In state.EndExecute(s)
                   data.Add(entity)
               Next entity
               Me.CustomersGrid.ItemsSource = data
           End Sub, query)
        End Sub
    End Class
    
  5. Run the application. You should see a data grid that is similar to the following.

Next Steps

This walkthrough has shown only the basic steps to create a project and retrieve some data from a data service. To learn about additional capabilities, check out the next walkthrough.