WCF (4), RIA Services

In this series of articles, we will discuss the major features of WCF:

Introduction


RIA stands for Rich Internet Applications. What is WCF RIA?

[WCF RIA Services is compatible with either .NET framework 4 or .NET Framework 4.5, and with either Silverlight 4 orSilverlight 5.]

Domain services are Windows Communication Foundation (WCF) services that encapsulate the business logic of a WCF RIA Services application. Adomain service exposes a set of related operations in the form of a service layer. When you define a domain service, you specify the data operations that are permitted through the domain service. --- Microsoft

It eases the procedure of building a complex business application in Silverlight by offering client-side validation, where Silverlight is a framework offered by Microsoft, it is a rich internet applications and available for useas a browser plug-in, just like Adobe Flash.

The following figure illustrates a part of the WCF architecture, where WCF RIA services has its focus in general. --- from

 
It seems it is quite hard to understand what WCF RIA Service is from the explanations above. This article will use a pare of sample apps of WPF and Silverlight to demo WCF RIA Service, and draw a clear picture on it.
 
  

A: Build a WPF app with Entity Framework


Build a WPF app with Entity Framework Database First approach, we follow the exact procedure from Entity Framework (2), With .Net MVC, Database-First, except the app is WPF, and we use Visual Studio 2010 the later is for our discussion later on about WCF RIA Service.
 

Step 1 - Create an WPF app

 
We use the Visual Studio 2010 and .NET Framework 4 to build the app::
  • Start Visual Studio and select Create a new project.
  • In the Create a new project dialog,
    • on the left side panel select Windows,
    • on the right Select WPF Application (.NET Framework)
    • Named as WpfApplication  > OK.
 
 

Step 2, Reverse Engineer Model

 
We’re going to make use of Entity Framework Designer, which is included as part of Visual Studio, actually called as ADO.NET Entity Data Model, to create our model.
  • Project -> Add New Item…
  • Select Data from the left menu and then ADO.NET Entity Data Model
  • Enter StoreModel as the name and click Add  
 
Then, following the steps in the article Entity Framework (2), With .Net MVC, Database-First, we got the result:
 
 
Because we build WPF app, and we use Visual Studio 2010, the result is a little bit different from one, in the avove article, that is MVC module in VS 2019:
  1.  The created entity only includes two files, one is .edmx file, showing the design mode of the entity; another is the designer code file: StoreModel.Designer.cs,
  2.  There is DataSource window showing the entity you choose (you may Click Data => Show Data Source to open it):
Wcf RIA Services 11
 

Step 3, Build the Grid, and Run the app

 
From the DataSource window, we can choose the control we want to bind in the front end, such as DataGrid:
 
 
Drag and drop the DataSoure into the window:
 
 
 
Run the app, we will get:
 
 
 

B: Build a Silverlight app with WCF RIA Service


Build a Silverlight app with Entity Framework Database First approach, we follow the exact procedure from this article, WCF - Ria Services (step 1~10), with the same project, service and data names. If any one is interested with the code, you can download it from another article, Silverlight 5 WCF RIA Services.
 
Step 1 - Create an Silverlight app
 
We use the Visual Studio 2010 and .NET Framework 4 to build the app::
  • Start Visual Studio and select Create a new project.
  • In the Create a new project dialog,
    • on the left side panel select Silverlight,
    • on the right Select Silverlight Application (.NET Framework)
    • Named as SLWCFRiaServices  > OK.
  • Choose Silverlight 5 and Check Enable WCF RIA Services => OK
We got two projects: One is Silverlight front end, named SLWCFRiaServices, another is the server (middle tier) named SLWCFRiaServices.Web, Note: a group of System.ServiceModel DLLs are added when we choose to Enable WCF RIA Service support.
 
 
 

Step 2, Reverse Engineer Model

 
We’re going to make use of Entity Framework Designer on the server project SLWCFRiaServices.Web:
  • Project -> Add New Item…
  • Select Data from the left menu and then ADO.NET Entity Data Model
  • Enter DataModel as the name and click Add 
 We do the same as what we did in A: Step 2 above for WPF, and finally, we got:
 
 
While different from WPF, after adding the entity into the web app, there is no DataSource object created. Now, it is where the WCF RIA Service jump into.
 

Step 3, Add Domain Service Class

 
We’re going to add a Domain Service Class on the server project SLWCFRiaServices.Web:
  • Project -> Add New Item…
  • Select Web from the left menu and then DomainService Class
  • Enter DataDomainService as the name and click Add 
  • Choose the data, enable editing  => OK
 
We got result:
 
 
There are two files created: DataDomainService.cs and DataDomainService.metadata.cs, actually, they are the DataContext class and Data Entity Class:
 
DataDomainService.cs --- DataContext class:
  1. namespace SLWCFRiaServices.Web  
  2. {  
  3.     using System;  
  4.     using System.Collections.Generic;  
  5.     using System.ComponentModel;  
  6.     using System.ComponentModel.DataAnnotations;  
  7.     using System.Data;  
  8.     using System.Linq;  
  9.     using System.ServiceModel.DomainServices.EntityFramework;  
  10.     using System.ServiceModel.DomainServices.Hosting;  
  11.     using System.ServiceModel.DomainServices.Server;  
  12.    
  13.     // Implements application logic using the PublishingCompanyEntities context.  
  14.     // TODO: Add your application logic to these methods or in additional methods.  
  15.     // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access  
  16.     // Also consider adding roles to restrict access as appropriate.  
  17.     // [RequiresAuthentication]  
  18.     [EnableClientAccess()]  
  19.     public class DataDomainService : LinqToEntitiesDomainService<PublishingCompanyEntities>  
  20.     {   
  21.         // TODO:  
  22.         // Consider constraining the results of your query method.  If you need additional input you can  
  23.         // add parameters to this method or create additional query methods with different names.  
  24.         // To support paging you will need to add ordering to the 'Articles' query.  
  25.         public IQueryable<Article> GetArticles()  
  26.         {  
  27.             return this.ObjectContext.Articles;  
  28.         }  
  29.     }  

DataDomainService.metadata.cs --- Data Entity Class
  1. namespace SLWCFRiaServices.Web  
  2. {  
  3.     using System;  
  4.     using System.Collections.Generic;  
  5.     using System.ComponentModel;  
  6.     using System.ComponentModel.DataAnnotations;  
  7.     using System.Linq;  
  8.     using System.ServiceModel.DomainServices.Hosting;  
  9.     using System.ServiceModel.DomainServices.Server;  
  10.   
  11.     // The MetadataTypeAttribute identifies ArticleMetadata as the class  
  12.     // that carries additional metadata for the Article class.  
  13.     [MetadataTypeAttribute(typeof(Article.ArticleMetadata))]  
  14.     public partial class Article  
  15.     {  
  16.         // This class allows you to attach custom attributes to properties  
  17.         // of the Article class.  
  18.         //  
  19.         // For example, the following marks the Xyz property as a  
  20.         // required property and specifies the format for valid values:  
  21.         //    [Required]  
  22.         //    [RegularExpression("[A-Z][A-Za-z0-9]*")]  
  23.         //    [StringLength(32)]  
  24.         //    public string Xyz { get; set; }  
  25.         internal sealed class ArticleMetadata  
  26.         {  
  27.             // Metadata classes are not meant to be instantiated.  
  28.             private ArticleMetadata()  
  29.             {  
  30.             }  
  31.   
  32.             public int ArticleID { getset; }   
  33.             public Nullable<int> AuthorID { getset; }   
  34.             public string Body { getset; }  
  35.             public string Title { getset; }  
  36.         }  
  37.     }  

 
But we still have not seen the DataSource object. However, if we swich to the Silverlight Project, the Data source object is showing up:
 
 
Actually, this is what we choose to Enable WCF RIA Service support to do, otherwise, it won't happen.
 
 

Step 4, Build the Grid, and Run the app

 
Exactly the same as WPF project, we add a grid, and run the app:
 
 
 

C: Understanding of WCF RIA Service


Now, we may have a better understanding of WCF RIA Service:
  •  It works on Silverlight
  •  It brings middle tier entity classes to client side without duplicating the classes.
  •  Middle tier Entity classes are built up by Entity Framework, or say, ADO.NET Entity Data Model.
  •  The Bridge to link middle tier and client side is the Domain Service Class that supplys a WCF services.

WCF RIA Domain Service

 
The following figure shows the architecture of a WCF RIA Domain Service − from
 
WCF RIA Services – Querying Data

The following figure shows how a query is created on the client side and executed on the server side to return queryable results. DAL stands for Data Access Layer. from

 

Summary

 
This article uses a sample to demo WCF RIA Service, the major conclusions are
  •  It works on Silverlight
  •  It brings middle tier entity classes to client side without duplicating the classes.
  •  Middle tier Entity classes are built up by Entity Framework, or say, ADO.NET Entity Data Model.
  •  The Bridge to link middle tier and client side is the Domain Service Class that supplys a WCF services. 
References