Learn WCF RIA Service: Day 2

Learn WCF RIA Service: Day 1

Building a simple line of business Application using RIA Services

Last day we had enough of theory on RIA Services. Today let us get our hand dirty and launch visual studio. We will keep it simple and easy in flow since it is our first exercise.

Essentially we will fetch data from Person table and bind it to grid view on Silverlight page. Flow diagram of the application we are going to make would be as below diagram.


Building Silverlight Application

Start with creating a Silverlight application.


Next you will get prompted to choose

  1. Web site to host Silverlight application
  2. Select Silverlight target version

And if you have WCF RIA services installed then you will get a check box to instruct Silverlight whether you want to enable WCF RIA Service for this particular Silverlight application or not ? You want to enable WCF RIA service so check the checkbox Enable WCF RIA Services.


We have instructed Silverlight application while creating that we want RIA Service enabled and next we need to create Data Model to be exposed and share data through this model in between presentation layer and middle layer.

Building Data Model

To create data model right click on web project [project with extension .web] and choose option add new item from context menu.


Next from Data tab you need to choose ADO.Net Entity Model template. We are going to use ADO.Net Entity Model ORM to create data model. ADO.Net Entity Model creates model with extension edmx. Change name of created edmx file to SchoolModel.edmx.


After clicking on add button, you will be prompted to choose the source of data model. Either you can create empty data model or choose a database as a source to create data model. We are going to choose School database residing on my local server as a source of data model. So select Generate from database option and click on next button


Now you will be prompted to choose database object. If school database is not listed in drop down then you can create a New Connection else you can choose from the dropdown. Let us proceed with creating a new connection

On clicking of New Connection button, you will be prompted with dialog box to specify database connection properties. Specify different values as of below image and click on Test Connection. After getting successful message click on Ok button.


In Data connection dialog box if you want you can change the name of connection setting in web.confg. However I am leaving the default name.


Click on next button to get last dialog box to conclude data model creation. Here you need to choose database objects you want to put as mart of data model. You can have tables, views, and stored procedures from database as part of data model. I am selecting all tables and stored procedures as part of data model. If you want you can change namespace name but for sake of simplicity at this point leave everything else as default.


On click on Finish button you will get data model created and you will get data model file open in designer surface. Here you can notice mapped between the entities.


In solution explorer you will find SchoolModel.edmx file has been added under the web project.


Building Domain Service

To create data model right click on web project [project with extension .web] and choose option add new item from context menu.


Next from Web tab you need to choose Domain Service class template. Change name to SchoolRIAService.cs and click on Add button.


In next dialog box go ahead and

  1. Select DataContext or ObjectContext class from the dropdown. You will get SchoolEntities context class in drop down value to choose.
  2. You can edit Domain Service class name. For simplicity leave it as it is.
  3. You have check box to specify Enable Client Access. Check this check box.
  4. You have check box to specify whether you want to expose ODATA Endpoint. For this time uncheck it.
  5. You will get all the entities from the context class listed. You can choose from them to be exposed as RIA Service. I am selecting all the entities and enabling all entities for edit.
  6. Check the box generates associated class for metadata.


Click on OK button and give a second to examine web project in solution explorer. You will find three classes added there. We will examine and talk more on these classes in coming days. Today we are just going to see there uses at the presentation layer in Silverlight project.


Using Domain Service in Presentation layer or Silverlight client

You have,

  1. Created data model
  2. Create domain service

Now build the web project and add below namespace on MainPage.xaml.cs file.


After adding namespaces open MainPage.xaml and drag and drop a data grid on that. Give name of the data grid as grdRIADataGrid. Xaml would be as below,


<UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  x:Class="DemoDay2.MainPage"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
    <sdk:DataGrid x:Name="grdRIADataGrid" />

At this point designing of page has been completed. You need to bind data from person table to data grid. For that

1. Create instance of domain service class


2. Load all people to the context of client side proxy


3. Fetch the entities from loaded list of entities and bind as item source of data grid


Code behind would look like as below,


using System.Windows.Controls;
using DemoDay2.Web;
using System.ServiceModel.DomainServices.Client;
namespace DemoDay2
    public partial class MainPage : UserControl
        public MainPage()
            SchoolRIAContext proxy = new SchoolRIAContext();
            LoadOperation<Person> loadPerson = proxy.Load(proxy.GetPeopleQuery());
            grdRIADataGrid.ItemsSource = loadPerson.Entities ;

Now you run Silverlight project and in data grid you will get the entire person from school database using RIA Service.


Today we followed a simple path and fetched data from database and bind to data grid on at Silverlight client using RIA Service. We have not gone deeper into different aspect of context class. We will discuss then in further days. I will conclude day two with assumption that by now you know,

  1. Why WCF RIA?
  2. Creating simple line of business application using RIA Services

See you on day 3 Smile

Learn WCF RIA Service: Day 1

7 responses to “Learn WCF RIA Service: Day 2”

  1. A DL-link to your school DB would be nice… greate series though! 🙂

  2. how do i return List of object which are not entities in EF. I m getting the data from in that object from direct sql using simple sql query or i can fetch the same from entity. making the List of the objects but i m not able to return the List. how can i do via wcf rai services

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s