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
- In Visual Studio, create a new Silverlight project by selecting File, New, and then Project. The New Project dialog box appears.
-
Select the Silverlight Application template and name the new project OA.SL.WcfData.Demo.
Click OK. The New Silverlight Application dialog box appears.
-
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
- 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.
- In the list of categories, select Data and then select the Telerik Data Access Domain Model item.
- Name the new file SofiaCarRentalDomainModel.rlinq and click Add. The Telerik Data Access Create Model Wizard appears.
- In the Select Domain Model Type screen, select Populate from database. Enter SofiaCarRentalDbContext for model name.
- In the Setup Database Connection screen, create a data connection to the SofiaCarRental database and click Next.
-
In the Choose Database Items screen, select the all tables.
-
Click Finish. Persistent classes are generated for the tables.
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
- Build your solution.
-
Right-click the SofiaCarRentalDomainModel.rlinq and select Add Telerik Data Access Service....
-
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.
-
Click Next to continue. The Select Service Type dialog appears. Select WCF Data Service v3.
-
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.
-
Click Next to continue. In the Preview dialog, review all changes that will be done in your project.
-
Click Finish to generate the service.
-
The Service Wizard performs the following steps:
- Sets references to all of the WCF Data Services libraries.
- Sets references to the Telerik Data Access DLLs.
-
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:
- 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.
- In the Namespace text box, type SofiaCarRentalWcfDataService, and then click OK.
-
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
- Open MainPage.xaml.
-
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>
Open the code-behind for MainPage.xaml.
-
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
-
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.