Book a Demo Free Trial

Building a .Net RIA Service Project

Gaurav Mantri

Sep 2, 2009

Category: General

.Net RIA Services bring together the ASP.NET and Silverlight platforms. It provides a pattern to write application logic that runs on the mid-tier. It integrates with Silverlight components on the client and ASP.NET on the mid-tier. Get more information about RIA services from http://www.nikhilk.net/NET-RIA-Services-Vision-Architecture.aspx. In this blog we see how to create a simple .NET RIA Service enabled project. It only provides a brief overview of.Net RIA Services.

Step 1: First Create a new Silverlight Project as:

step1

Step 2: Then enable .Net Ria Services like this:

step2

Step 3: Now to mid-tier i.e., on RiaTest.Web project add a new item and select the Data category as follows:

step3_1

we will access the Northwind database using the Entity Framework. We choose 2 tables from the northwind for this application.

step3_2

Step 4: After building project create a DomainService on the mid-tier. A DomainService is a class that exposes entities and operations for a specific data domain. It is also where the developer adds application logic. To create a Domain Service Add new item to the mid-tier and in the Web category add DomainService class as follows:

step4

Step 5: Now we select only one table Orders, we will not set Enable Editing for the table because we want only read only table. Generate metada will create metadata of table on client.

step5_1

It will generate a class as follows:

step5_2

It has following characteristics:

  • It is derived from LinqToEntitiesDomainService, an abstract base class built into the .NET RIA service framework3.
  • It is bound to NorthwindEntities1 class we created earlier
  • A single GetOrders() query method is generated because we select only Orders table
  • This.Context.Orders in GetOrders() will give all the order in table.
  • OrderService is marked with [EnableClientAccess] indicating it is visble on client tier

When we build project and click on Show all files on client tier we see some Gnerated code as:

step5_3

This is the client proxy class which is generated from the Domain service class which we create on mid-tier.

Step 6: Now in MainPage.xaml add a data Grid named “MyGrid” as follows:

step6

Don’t forget to include System.Controls.Data.dll in references.

Step 7: Now in MainPage.xaml.cs add 2 references :

  • System.Windows.Ria.Data
  • RiaTest.Web

Now create an object of OrderContext and then load entities of Orders and set the itemsource of the datagrid.

step7

When you run the project you see all the orders in the data grid.