WCF RIA service is a framework to develop n-tier application for Rich Internet Application (RIA). It is mainly used in RIA applications like Silverlight, AJAX client, etc. It solves the major problem while developing business application like decoupling the resource access, application logic and presentation layer. WCF RIA service was introduced in Silverlight 4 with .net framework 4, and it can be developed using visual studio2010.
Main problem developer are facing while developing the n-tier RIA application will be coordinating the application logic between middle tier and presentation tier. This problem will be solved by using WCF RIA service, it will synchronize the code between middle and presentation tier.
WCF RIA service will allow developer to write the set of service code and this server code will be available to the client side without manually duplicate that programming logic. RIA service client will be updated with business rule and entity present at the server side, when your recompile your project solution.
WCF RIA service will generate the code at the client side related to the service and domain entities declared at the server side.
RIA service exposes the data from server side to client side using Domain service, RIA service framework implements the each domain service as WCF service to access the data as business entity.
-
WCF RIA Domain Service
-
Problems solved in RIA Service
-
Query/Update process in RIA
-
How to create Silverlight-WCF RIA service
fig: WCF RIA Serive architecture
Domain Service
Domain services are WCF services that expose the business logic of a WCF RIA Services application. Domain service contains set of business related data operation and it is exposed as WCF service.
Below diagram explains integration of the RIA service with WCF
The DomainService class is the base class for all classes that serve as domain services.
- DomainServiceHost is the hosting class for domain service; internally
- DomainServiceHost uses the WCF ServiceHost class to host the application.
A domain service class must be marked with the EnableClientAccessAttribute attribute to make the service available to the client project. The EnableClientAccessAttributeattribute is automatically applied to a domain service when you select the Enable client access check box in the Add New Domain Service Class dialog box. When the EnableClientAccessAttribute attribute is applied to a domain service, RIA Services generates the corresponding classes for the client project.
1: //Example:
2: [EnableClientAccess()]
3: public class EmployeeDomainService : DomainService
4: {
5: private EmployeeData data = EmployeeData.Instance;
6:
7: public IEnumerable < Employee> GetEmployees()
8: {
9: return data.EmployeeList;
10: }
11: }
12:
DomainContext class at the client side is used to consume the Domain service by using DomainClient object. DomainContext class available inside the name space "System.ServiceModel.DomainServices.Client"
fig: WCF RIA Domain Serive architecture
Problem solved in RIA
- To have best performance of the RIA application, app logic need to be available in client and server side. This problem is solved by auto generating the code at the client side while recompiling the project.
- Asynchronous call – Asynch service call are supported in Domain service by using WCF infrastructure
- Handling large data and data across different tier – Large amount of data can be access and filter using IQueryable object. Since entity objects used in domain service are serializable and so it can be access across different layer
- Security/Access control – ASP.Net membership frameworks are integrated with RIA service to provide security systems to RIA service
- Validation – Entity can be validated based using adding attribute to the class members
1: //Example:
2: public class Member
3: {
4: [Key]
5: public int MemberId { get; set; }
6:
7:
8: public string Fname { get; set; }
9:
10: [Required]
11: public string Lname { get; set; }
12:
13: public DateTime JoinDate { get; set; }
14:
15: [Range(30,90, ErrorMessage="sorry, you are either too young or too old for our club!")]
16: public int Age { get; set; }
17: }
Querying/Updating data in RIA Service
The below diagram are self explanatory to discuss about the querying or updating the data using RIA service
fig: WCF RIA to Query data
fig: WCF RIA to update data
How to Create WCF RIA Service
Download:
Silverlight_WCF_RIA_Service.zipLet us understand more about the WCF RIA service by creating Silverlight client application which read and updated the Employee details from WCF RIA Service.
Step 1:Start the Visual Studio 2010 and click File -> New-> Project. Enter the project name and click “Create”
Step 2:Select “Enable WCF RIA Services”. This will make your Silverlight application to user WCF RIA service
Step 3:Create “Data” folder and add DataModel” class as shown below. This is the data class which will return list of Employee and update the employee list
Data Model class:
1: public class Employee
2: {
3: [Key]
4: public int EmpId { get; set; }
5: public string Fname { get; set; }
6: public string Lname { get; set; }
7: public DateTime JoinDate { get; set; }
8: public int Age { get; set; }
9: }
10:
11:
12: public partial class EmployeeData
13: {
14: private static readonly EmployeeData _instance = new EmployeeData();
15: private EmployeeData() { }
16: public static EmployeeData Instance
17: {
18: get
19: {
20: return _instance;
21: }
22: }
23:
24:
25: private List < Employee > empList = new List < Employee>()
26: {
27: new Employee() { EmpId = 1, Fname = "Sam", Lname = "kumar",
28: JoinDate=new DateTime(2010,7, 21), Age=30},
29: new Employee() { EmpId = 2, Fname = "Ram", Lname = "kumar",
30: JoinDate=new DateTime(2009,6,8), Age=35},
31: new Employee() { EmpId = 3, Fname = "Sasi", Lname = "M",
32: JoinDate=new DateTime(2008,3,5), Age=39},
33: new Employee() { EmpId = 4, Fname = "Praveen", Lname = "KR",
34: JoinDate=new DateTime(2010, 5,1), Age=56},
35: new Employee() { EmpId = 5, Fname = "Sathish", Lname = "V",
36: JoinDate = new DateTime(2006,12,15), Age=72},
37: new Employee() { EmpId = 6, Fname = "Rosh", Lname = "A",
38: JoinDate=new DateTime(2009,2,2), Age=25}
39: };
40:
41: public IEnumerable< Employee > EmployeeList
42: {
43: get
44: {
45: return empList;
46: }
47: }
48:
49:
50: public void Update(Employee updEmployee)
51: {
52: Employee existing = empList.Find(p => p.EmpId == updEmployee.EmpId);
53: if (existing == null)
54: throw new KeyNotFoundException("Specified Employee cannot be found");
55:
56: existing.Fname = updEmployee.Fname;
57: existing.Lname = updEmployee.Lname;
58: existing.JoinDate = updEmployee.JoinDate;
59: existing.Age = updEmployee.Age;
60: }
61: }
Step 4:To expose the Employee related operation to the client side, Create domain service class. By right click project file and select Add new item.
Step 5:Add code to return the Employee list
Domain Service class:
1: // TODO: Create methods containing your application logic.
2: [EnableClientAccess()]
3: public class EmployeeDomainService : DomainService
4: {
5: //Create instance of the Data access layer
6: private EmployeeData data = EmployeeData.Instance;
7:
8: public IEnumerable< Employee> GetEmployee()
9: {
10: return data.EmployeeList ;
11: }
12:
13: public void UpdateEmployee(Employee emp)
14: {
15: data.Update(emp);
16: }
17: }
Step 6:Compile the solution – After compilation RIA service will generate the application logic at the client side using DomainContext object. Enable show all files option for the solution and view the auto generated code.
Step 7:View the DomainContext class are created at the client side.
Domain Context class at client:
1: ///
2: /// The DomainContext corresponding to the 'EmployeeDomainService' DomainService.
3: ///
4: public sealed partial class EmployeeDomainContext : DomainContext
5: {
6:
7: #region Extensibility Method Definitions
8:
9: ///
10: /// This method is invoked from the constructor once initialization is complete and
11: /// can be used for further object setup.
12: ///
13: partial void OnCreated();
14:
15: #endregion
16:
17:
18: ///
19: /// Initializes a new instance of the < see cref="EmployeeDomainContext"/> class.
20: ///
21: public EmployeeDomainContext() :
22: this(new WebDomainClient< IEmployeeDomainServiceContract>(new
23: Uri("MyFirstRIAApplication-Web-EmployeeDomainService.svc",
24: UriKind.Relative)))
25: {
26: }
27:
28: ........
29: ........
Step 8:Add DataGrid to Main.xaml file to display the employee details query from DataModel and add two buttons to update and reject the data changed from client side.
Main.xaml
< Grid x:Name="LayoutRoot" Background="White">
< StackPanel Orientation="Vertical" HorizontalAlignment="Left" >
< sdk:DataGrid x:Name="EmployeeGrid" AutoGenerateColumns="True"
RowEditEnded="EmployeeGrid_RowEditEnded" />
< Button Content="Accept" Height="23" Name="btnAccept"
Width="75" Margin="5" Click="btnAccept_Click" />
< Button Content="Reject" Height="23" Name="btnReject"
Width="75" Margin="5" Click="btnReject_Click"/>
</StackPanel>
</Grid>
Main.xaml.vb
1: public partial class MainPage : UserControl
2: {
3: //create instance of Doman context class
4: EmployeeDomainContext ctx = new EmployeeDomainContext();
5:
6: public MainPage()
7: {
8: InitializeComponent();
9: //Load query data , Read data from DAL layer to UI
10: EntityQuery< Employee> query = ctx.GetEmployeeQuery();
11: LoadOperation< Employee> lo = ctx.Load< Employee>(query);
12: EmployeeGrid.ItemsSource = lo.Entities;
13: }
14:
15: private void EmployeeGrid_RowEditEnded(object sender,
16: DataGridRowEditEndedEventArgs e)
17: {
18:
19: }
20:
21: private void btnAccept_Click(object sender, RoutedEventArgs e)
22: {
23: //Update the DAL with user changes
24: ctx.SubmitChanges();
25: }
26:
27: private void btnReject_Click(object sender, RoutedEventArgs e)
28: {
29: //Roll back the user changes
30: ctx.RejectChanges();
31: }
32: }
Step 9:Run the application and see the output as shown below