Walkthrough: Creating a RIA Services Solution

[WCF RIA Services Version 1 Service Pack 2 is compatible with either .NET framework 4 or .NET Framework 4.5, and with either Silverlight 4 or Silverlight 5.]

In this walkthrough, you create a WCF RIA Services application that retrieves data from the AdventureWorksLT database and then makes it available to a Silverlight client where it is presented. You access the data from the data source by creating entity classes that represent various database tables on the server in the middle tier and then you present it by creating a domain service that makes these middle tier entities accessible to the Silverlight client. This walkthrough serves as the starting point for many of the other walkthroughs in the RIA Services documentation.


This and the other walkthroughs presented in the RIA Services documentation require several prerequisite programs, such as Visual Studio 2010 and the Silverlight Developer Runtime and SDK, be installed and configured properly, in addition to WCF RIA Services and the WCF RIA Services Toolkit. They also require installing and configuring SQL Server 2008 R2 Express with Advanced Services and installing the AdventureWorks OLTP and LT database.

Detailed instructions for the satisfaction of each of these prerequisites are provided by the topics within the Prerequisites for WCF RIA Services node. Follow the instructions provided there before proceeding with this walkthrough to ensure that you encounter as few problems as possible when working through this RIA Services walkthroughs.

To set up a RIA Services solution

  1. Create a new RIA Services project in Visual Studio 2010 by selecting File, New, and then Project.

    The New Project dialog box appears.

  2. Select the Silverlight Application template from Silverlight group of the Installed Templates and name the new project RIAServicesExample.


  3. Click OK.

    The New Silverlight Application dialog box appears.

  4. Select the Enable WCF RIA Services check box near the bottom of the dialog box. Checking this box creates a RIA Services link between the client project and the server project.


  5. Click OK to create the solution.

    The solution contains two projects: a client project and a server project. The client project is named RIAServicesExample and it contains the Silverlight code that you use to create the presentation tier. The server project is named RIAServicesExample.Web and it contains the middle-tier code.


Creating Data Models

In this section, you will create the ADO.NET Entity classes that represent data from the AdventureWorksLT database. RIA Services works with a variety of data modeling classes and data sources. For more information on the options for accessing data with RIA Services, see the Data topic.


When using an Entity Data Model (EMD) with Visual Studio 2010, you must select the Include foreign key columns in the model option. By default, this option is selected when you use the Entity Data Model wizard. You also must use the default behavior of embedding EMD mapping information in the assembly.

To make data available in the middle tier

  1. In Solution Explorer, right-click the server project, RIAServicesExample.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 ADO.NET Entity Data Model template.

  3. Name the new file AdventureWorksModel.edmx and click Add.

    The Entity Data Model Wizard appears.

  4. In the Choose Model Contents screen, select Generate from database option and click Next.

  5. In the Choose Your Data Connection screen, create a data connection to the database and click Next.

  6. In the Choose Your Database Objects screen, select the Address, Customer, and CustomerAddress tables.

  7. Confirm that the Include foreign key columns in the model check box is checked by default and click Finish.

    Entity models are created for the tables.

  8. Build (with the key combination Ctrl+Shift+B) the solution.

Creating a Domain Service

In this section, you will add a domain service to the middle-tier project. A domain service exposes the data entities and operations in the server project to the client project. You can add business logic to the domain service to manage how the client interacts with the data.

To create the domain service

  1. Right-click the server project, select Add and New Item.

  2. In the list of categories, select Web and then select the Domain Service Class template.

  3. Name the class CustomerDomainService.cs (or CustomerDomainService.vb).


  4. Click Add.

    The Add New Domain Service Class dialog box appears.

  5. Make sure that the Enable client access box is checked.

  6. Select the Customer entity and then check the Enable Editing box for it.


  7. Click OK.

    The CustomerDomainService class is generated in a new CustomerDomainService.cs (or CustomerDomainService.vb) file.

  8. Open this file. Notice that the file has the following characteristics:

    • The CustomerDomainService class derives from LinqToEntitiesDomainService<TContext> class, which is an abstract base class in the RIA Services framework. This base class was used automatically because the domain service exposes an ADO.NET Entity data class.

    • The generic base class is bound to the entity class that was created in previous steps by the AdventureWorksLTEntities of type ObjectContext in its generic parameter.

    • The CustomerDomainService class is marked with the EnableClientAccessAttribute attribute to indicate that it is visible to the client tier.

    • A query method named GetCustomers is generated. This method returns every item without any filtering or sorting.

    • Methods to insert, update, and delete customers from the records have been generated.

Creating the Silverlight Client

In other walkthroughs, you will be shown how to add business logic to the domain service. For this walkthrough, you will simply use the GetCustomers method generated by default.

Client proxy classes are generated when you built the solution. The RIA Services link that was established between the client project and the server project make this code generation possible. These client proxy classes provide access to the data from the client.

To see the generated client proxy classes

  1. Build the solution.

    When you build the solution, code is generated in the client project.

  2. In Solution Explorer, select the RIAServicesExample client project and click the Show All Files icon at the top of the window.

    Notice the Generated_Code folder contains a RIAServicesExample.Web.g.cs (or RIAServicesExample.Web.g.vb) file.


  3. Open the code file in the Generated_Code folder.

    Notice that the file has the following characteristics:

    • A WebContext class that derives from the WebContextBase class is generated.

    • A CustomerDomainContext class that derives from the DomainContext class is generated. This class has a method named GetCustomersQuery that corresponds to the query method created in the domain service.

    • A Customer class that derives from the Entity class is generated for the entity exposed by the domain service. The Customer entity class in the client project matches the Customer entity on the server.

To display the data in the Silverlight client

  1. Open MainPage.xaml.

  2. From the Toolbox on the left, drag a DataGrid control to within the Grid element in XAML view.

    Dragging the DataGrid control from the Toolbox causes a namespace using System.Windows.Controls statement and a reference to a System.Windows.Controls.Data assembly to be added automatically.


    If you add the DataGrid without dragging it from the Toolbox, you must add the reference to the System.Windows.Controls.Data assembly to the client project and the using statement manually in the code behind file.

  3. Change the value of the AutoGeneratedColums to True, name the DataGrid element CustomerGrid, and adjust the height and width attributes as shown in the following XAML.

        d:DesignHeight="300" d:DesignWidth="400">
        <Grid x:Name="LayoutRoot" Background="White">
          <my:DataGrid Name="CustomerGrid"></my:DataGrid>
  4. Open the code-behind for MainPage.xaml.

  5. Add using (C#) or Imports (Visual Basic) two statements: using RIAServicesExample.Web; and the using System.ServiceModel.DomainServices.Client;.

    The RIAServicesExample.Web namespace is the namespace containing the generated code for the client project in the RIAServicesExample.Web.g.cs (or RIAServicesExample.Web.g.vb).

  6. To instantiate the CustomerDomainContext, add the line of code private CustomerDomainContext _customerContext = new CustomerDomainContext(); in the MainPage class.

    Imports System.Windows.Ria
    Imports RIAServicesExample.Web
    Partial Public Class MainPage
        Inherits UserControl
        Private _customerContext As New CustomerDomainContext
        Public Sub New()
            Dim loadOp = Me._customerContext.Load(Me._customerContext.GetCustomersQuery())
            CustomerGrid.ItemsSource = loadOp.Entities
        End Sub
    End Class
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using RIAServicesExample.Web;
    using System.Windows.Ria;
    namespace RIAServicesExample
        public partial class MainPage : UserControl
            private CustomerDomainContext _customerContext = new CustomerDomainContext();
            public MainPage()
                LoadOperation<Customer> loadOp = this._customerContext.Load(this._customerContext.GetCustomersQuery());
                CustomerGrid.ItemsSource = loadOp.Entities;
  7. Retrieve customer entities by calling the GetCustomersQuery method with LoadOperation<TEntity>: LoadOperation<Customer> loadOp = this._customerContext.Load(this._customerContext.GetCustomersQuery());.

  8. Bind the entities loaded to the DataGrid with CustomerGrid.ItemsSource = loadOp.Entities;.

    To summarize, the MainPage.xaml.cs file should now contain the following code:

    //Namespaces added
    using RIAServicesExample.Web;
    using System.ServiceModel.DomainServices.Client;
    namespace RIAServicesExample
        public partial class MainPage : UserControl
            private CustomerDomainContext _customerContext = new CustomerDomainContext();
            public MainPage()
                LoadOperation<Customer> loadOp = this._customerContext.Load(this._customerContext.GetCustomersQuery());
                CustomerGrid.ItemsSource = loadOp.Entities;
  9. Run (F5) 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 unfiltered data from a domain service. Here are some suggestions for learning about additional capabilities: