WCF RIA Service Tutorial-Part 01

No.of Views2547
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  19 Jan 2011 09:01:57
Tag : WCF , REST Services
It may be bit late but let us learn WCF RIA together. I will be writing many parts on Learning WCF RIA Services.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

It may be bit late but let us learn WCF RIA together. I will be writing many parts on Learning WCF RIA Services.

Part1

In this part just to get a feeling of WCF RIA and see our one application is running on WCF RIA, I am going to walkthrough creating first application through WCF RIA Services.
In a Silverlight Data Grid we will fetch and display all the records from Person table of School database using WCF RIA Service.

Creating Project

Open Visual studio and create a new a new Silverlight Application. 

Image Loading

Make sure you have checked the Enable WCF RIA Service checkbox. 

Image Loading

We will have two projects in the solution

Creating DataModel

Now let us create a DataModel.  I am going to use School database here.
The first step is to create database. We are going to use School database.  Script of sample School Database copy from here
Right click on Server Project [web project] and add a new item 

Image Loading

Add ADO.Net Entity model from Data tab.

Image Loading

Choose the option generate from Database.  If Database you want to use is listed in drop down the choose that else create a New Connection. 

Image Loading

Provide server name and authentication and choose the database.

Image Loading
Image Loading

Either leave default name for connection string in config file or give a unique name.  And after selecting table click finish.

Image Loading

Note: Make sure after creating Data Model you have built the project. This is a important step

Creating Domain Service

Right click on Server Project [web project] and add a new item. From web tab select Domain Service class

Image Loading

Select available Data context class from drop down.  And select Person class from list of tables. 

Image Loading

After this step you will get DomainService1.cs class created in the web project. 

Image Loading

There are two things to notice about the class.

1.    Class is inherited from LinqToEntitiesDomainService<T> class
2.    Attributed with EnableClientAccess.

Fetching Data from at Silverlight client

Build the project and add the namespace 

Image Loading

Add a Data Grid on design

Image Loading

After adding Data Grid xaml will look like below

MainPage.Xaml

<UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  x:Class="RIA1.MainPage"
    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"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    <Grid x:Name="LayoutRoot" Background="White">
        <sdk:DataGrid Name="myGrid" >
         </sdk:DataGrid>            
    </Grid>
</UserControl>

At the code behind now we need to fetch the data from Domain service.  For that

1.    Create an instance of Domain service class

Image Loading

2.    Load the entity set to be bind as item source of data grid.

Image Loading

MainPage.xaml.cs

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 RIA1.Web;
using System.ServiceModel.DomainServices.Client; 
 
namespace RIA1
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            DomainService1 context = new DomainService1();
            LoadOperation<Person> loapPerson = context.Load(context.GetPeopleQuery());
            myGrid.ItemsSource = loapPerson.Entities; 
 
        }
    }
}

Press F5 to run the application,

Image Loading

That's all,Hopes help and happy coding.

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top