Silverlight 3 and RIA Services - Part I

Posted by: Malcolm Sheridan , on 8/13/2009, in Category Silverlight 2, 3, 4 and 5
Views: 30266
Abstract: In part I of this article I’ll demonstrates how to use the new RIA services with Silverlight 3 to display data.
Silverlight 3 and RIA Services - Part I
 
RIA Services is a new offering from Microsoft that aims to simplify web n-tier architecture. The official wording from Microsoft is:
“Microsoft .NET RIA Services simplifies the traditional n-tier application pattern by bringing together the ASP.NET and Silverlight platforms. The RIA Services provides a pattern to write application logic that runs on the mid-tier and controls access to data for queries, changes and custom operations. It also provides end-to-end support for common tasks such as datavalidation, authentication and roles by integrating with Silverlight components on the client and ASP.NET on the mid-tier.”
The benefit of using RIA Services is felt when you’re using Silverlight 3 that interacts heavily with a database. All of your CRUD (Create, Read, Update and Delete) work is made easier than before. Part one of this article takes you through creating a Silverlight 3 web application and displaying data from the database. Part two will show you how to update, insert and delete data.
Before beginning you must install the latest RIA Services package from Microsoft. As of writing this the July 2009 version is the latest version. You can download it from here. I recommend that you also download the accompanying PDF, which provides a lot of detail, and plenty of good code samples.
To begin with open Visual Studio 2008 and choose File > New > Project > Silverlight > Silverlight Application. A New Silverlight Application dialog will appear asking about the web application tied to this project. At the bottom of the dialog is a new section called Enable .NET RIA Services. Tick this box to enable RIA Services for the Silverlight project:
 
New_Silverlight_Application
 
Open the web application and add a new ADO.NET Entity Data Model file to your project and name it Northwind.edmx. This example will connect up to the Northwind database. You can download it from here.
 
Add_New_Item
 
Connect to the Northwind database and select the Customers table from the Choose Your Database Objects dialog:
 
Entity_Data_Model_Wizard
 
Click Finish when you’re done. Now that you’ve added your Customer object you must build your project. This is a must because the next step we will be adding a Domain Service Class file to the project, and it needs to discover the Customers object you just added. The next step is to right click on the web application and choose Add > New Item > Domain Service Class. 
 
Domain_Service_Class  
 
The domain service class is a new file that is specifically for RIA Services. Name the file NorthwindService and click Add. The Add New Domain Service Class dialog appears. Select the Customer object and click OK:
 
Add_New_Domain
 
The file is created. The first thing you’ll notice is that class has been decorated with the EnableClientAccess attribute. This is required for Silverlight to be able to consume the service. A default method called GetCustomers has been created for you:
 
C#
 
public IQueryable<Customer> GetCustomers()
{
return this.Context.Customers;
}
 
VB.NET
 
Public Function GetCustomers() As IQueryable(Of Customer)
Return Me.Context.Customers
End Function
 
This is all that’s required for the web application. Open up the Silverlight application and add a reference to the System.Windows.Ria.Controls assembly:
 
Add_Reference
 
This reference is required because I am going to use a new control that is part of the RIA services installation called the DomainDataSource control. I think of this control as being similar to the ASP.NET ObjectDataSource. Add the following xaml to the MainPage.xaml file:
 
<UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  x:Class="SilverlightApplication1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:web="clr-namespace:SilverlightApplication1.Web"       
    xmlns:riaControls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Ria.Controls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
 <Grid x:Name="LayoutRoot">
        <riaControls:DomainDataSource QueryName="GetCustomers"
                                  AutoLoad="True"
                                  x:Name="dds">
            <riaControls:DomainDataSource.DomainContext>
                <web:NorthwindContext />
            </riaControls:DomainDataSource.DomainContext>
        </riaControls:DomainDataSource>
       
        <data:DataGrid ItemsSource="{Binding Data, ElementName=dds}">           
        </data:DataGrid>
    </Grid>
</UserControl>
 
The first step is to tell Silverlight that you’re using a control in the System.Windows.Ria.Controls assembly:
 
xmlns:riaControls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Ria.Controls"
 
Next add the DomainDataSource control and set the QueryName property to the name of the method, GetCustomers, in the domain service class. AutoLoad must be set to True for the data to be loaded automatically. Finally I am using Silverlight attached properties to set the DomainContext for the control to the NorthwindContext:
 
<riaControls:DomainDataSource.DomainContext>
<web:NorthwindContext />
</riaControls:DomainDataSource.DomainContext>
 
The NorthwindContext class is an object which is generated each time the project is compiled. To view this class, select the Silverlight project and show all the files. A Generated_Code folder has been created because this project is using RIA Services. Opening that folder you will see a file called SilverlightApplication1.Web.g.cs. In this file is the NorthwindContext class which implements the DomainContext class. The DomainContext object provides full access to all the functionality of the domain service:
 
Silverlight_Application
 

If you run the project you’ll see the data is displayed to the user in the DataGrid control. All of this has been achieved without having to write any additional code in the code behind file. The whole model of RIA Services is big and this article hopefully showed you how to use this great technology by getting a business application up and running quickly without having to write allot of code. In the next article I’ll show you how to update, insert and delete data. [Update]: The second part of this can be read over here Silverlight and RIA Services - Part II

The entire source code of this article can be downloaded from here

If you liked the article,  Subscribe to the RSS Feed or Subscribe Via Email

Malcolm Sheridan is an independent contractor who has been working with Microsoft technologies since VB4. Malcolm has worked with .NET since its inception and thoroughly enjoys ASP.NET.

Give a +1 to this article if you think it was well written. Thanks!
Recommended Articles
Malcolm Sheridan is a Microsoft awarded MVP in ASP.NET, a Telerik Insider and a regular presenter at conferences and user groups throughout Australia and New Zealand. Being an ASP.NET guy, his focus is on web technologies and has been for the past 10 years. He loves working with ASP.NET MVC these days and also loves getting his hands dirty with jQuery and JavaScript. He also writes technical articles on ASP.NET for SitePoint and other various websites. Follow him on twitter @malcolmsheridan


Page copy protected against web site content infringement by Copyscape


User Feedback
Comment posted by Scott on Saturday, August 15, 2009 12:27 AM
hi there,

It seems that this article has not included the pre-requisites or requirements in order to run Silverlight 3 + RIA Services on a server. Therefore, I have a couple of things to add and the followings are stuffs that must be installed on the server:

1. .NET3.5 Framework
2. Full Trust mode must be enabled
3. Install the Visual Web Developer 2008 tool
4. Install the Visual Studio 2008 Service Pack 1
5. Run the Silverlight 3 Toolkit
6. Run the RIA Service Installation
7. Silverlight can run on either IIS6 or IIS7

I am currently hosting my Silverlight 3 (+ RIA Service) with ASPHostCentral (http://www.asphostcentral.com/) and so far, everything works smoothly. To be honest, there are so many exciting things about this latest Silverlight 3, with an exceptional extra feature of RIA Services. I am still exploring further features, which include Expression Blend and hopefully, I can see more interesting articles here :)
Comment posted by Malcolm Sheridan on Saturday, August 15, 2009 5:15 AM
@Scott
The only thing I included was a download to the July 2009 preview of RIA services.  All those other items you mentioned have to be installed too.  I didn't add those as I was focusing on RIA itself.  
Comment posted by Spencer on Tuesday, August 18, 2009 12:12 PM
Veyr helpful for my college project. I need part2 please.
Comment posted by Malcolm Sheridan on Tuesday, August 18, 2009 7:47 PM
@Spencer
It is on its way!  
Comment posted by James on Wednesday, August 19, 2009 10:33 PM
Off Topic question: do you know how to add RIA services to an existing WebSite Application? I know how to add it to a ASP.NET Web Application, but I don't know how to add it to a WebSite application.  someone mentioned that I have to convert my website to web application, but i really don't want to if I don't have to.  thank you.
Comment posted by Malcolm Sheridan on Thursday, August 20, 2009 5:14 AM
@James
I always build my projects as web applications, so I can't answer that question.  If you try, please let me know how it goes.
Comment posted by Hasan Jhabuawala on Friday, September 4, 2009 2:57 AM
I want to fetch the data in the column in the last row and show it in a texblock how will i do that
Comment posted by Jon Chai on Saturday, September 26, 2009 11:05 PM
Hello Malcolm, nice article. Could you please direct me to the follow up? I can't seem to locate it. Thank you.
Comment posted by Carol on Sunday, September 27, 2009 12:01 AM
Jon: You can read the part 2 over here http://www.dotnetcurry.com/ShowArticle.aspx?ID=372
Comment posted by Jon Chai on Tuesday, September 29, 2009 12:02 AM
Thanks Carol
Comment posted by marrow on Saturday, November 7, 2009 2:28 PM
Using vs2008 and the same sl parts that are listed above (I think) I don't get a dialog like is shown offering a new Silverlight Application. I have (in vb) Silverlight Business Application and Silverlight Navigation Application. Why?

Also, the zip files that are offered for this project are password protected. That's been noted by another downloader on the site that offers the project files. So no where to go with this.
Comment posted by marrow on Saturday, November 7, 2009 11:55 PM
Using vs2008 and the same sl parts that are listed above (I think) I don't get a dialog like is shown offering a new Silverlight Application. I have (in vb) Silverlight Business Application and Silverlight Navigation Application. Why?

Also, the zip files that are offered for this project are password protected. That's been noted by another downloader on the site that offers the project files. So no where to go with this.
Comment posted by How do I place a Silverlight application on a host site on Sunday, January 10, 2010 7:39 PM
I am using asphostcentral to host webpages. Can some one tell me what I need to do so a Silverlight application create in Visual Studio can be displayed in on the server

You can email me at steve_44@inbox.com
Comment posted by Graham Tibbitts on Wednesday, February 17, 2010 4:00 PM
The assembly to add has been changed from System.Windows.Ria.Controls to System.Windows.Controls.Ria.
Comment posted by Bujen on Thursday, February 18, 2010 7:39 AM
thanks graham
Comment posted by Srinivasan on Monday, December 6, 2010 5:06 AM
I could not find the reference as System.Windows.Ria.Controls, so used System.Windows.Controls.Ria, but failed to give data control and control parameter through System.Windows.Controls.Ria. Please help

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel