Click here to Skip to main content
15,868,016 members
Articles / Web Development / ASP.NET

WCF RIA Services: ASP.NET Client Web Application

Rate me:
Please Sign up or sign in to vote.
4.84/5 (16 votes)
1 Jun 2010CPOL4 min read 71.1K   1.2K   42   10
Quick ASP.NET development using WCF RIA services

Table of Contents

  1. Introduction
  2. Background
  3. Prerequisites
  4. Step By Step Implementation
  5. Conclusion

Introduction

To check out the power of VS2010 is something that most of us are eagerly waiting to see it. Microsoft Tech - Ed in recent times has done a lot to showcase their remarkable products. It had all from SQL Server 2008 R2 to VS2010. The power of Windows Azure and WCF RIA services were the highlight of the show. My objective is to showcase some cool features of VS2010 and help developers to take full advantage of cool features that shipped with VS2010. Microsoft does say, Life runs on code, but here is the example where we can see hardly any code by developer and everything is automated to help one to create a website with delete, add, edit, sort and paging features. These take no code from the developer and it is just a few drag and drop to create a fancy RIA website.

Background

In VS2010 .NET 4.0, you get a whole lot of website templates and Silverlight template websites to start with. You can go ahead and customize this template as per your need. My intent is to help you to use this feature wherein one can create normal add, edit, sorting, paging and delete feature in Grid view within a few minutes. All you get is a readymade code to do this. This is only possible using WCF RIA services.

Prerequisites

'Ultimate' version of VS2010 has almost all features to start with.

There are four editions of VS2010:

  • Ultimate
  • Premium
  • Test
  • Professional

Supporting Operating Systems

  • Windows XP Sp3
  • Windows Vista SP2
  • Windows 7
  • Windows Server 2003 SP 2
  • Windows Server 2003 R2
  • Windows Server 2008 SP 2
  • Windows Server 2008 R2

Tutorial WCF RIA Service RC

Install Silverlight 4 SDK

Step By Step Implementation

Given below are the steps to create a web application using WCF RIA services.

Note: One needs to build(CTRL+Shift+B) the application after completion of each stated step.

1. Create Empty Website

Image 1

Add three references of WCF RIA Services:

  • Microsoft.Web.DomainServices.WebControls
  • System.ServiceModel.DomainServices.Hosting
  • System.ServiceModel.DomainServices.Server

Image 2

2. Create Data Model

Image 3

Go to Data tab and select ADOEntity Data Model in it. Select database and table. For given example here, I've created a sample customer table that contains two fields, CustomerID and CustomerName.

Image 4

Image 5

Image 6

Build the solution (CTRL+Shift+B).

3. Create Domain Service

Image 7

Now we need to create Domain services (DomainService.cs). Under web menu, select 'Domain Service Class'.The Domain services is a class that has contracts with data access layer. In this example, we've Customer as a domain class and we've services related to Customer like AddCustomer, EditCustomer and DeleteCustomer.

Build the solution (CTRL+Shift+B).

4. Bind DomainService with Datamodel

Image 8

Select tblCustomer entities and click checkbox to enable 'Editing' feature. Click Ok to proceed further.

Build the solution (CTRL+F5). It will throw a runtime error. As we enabled paging, we need to update few lines of code.

5. Drag Domain Datasource

Once we have the data model and domain service ready, we need to bring this model into view layer. For this, we need to drag DomainDataSource. We need to understand this hierarchy thoroughly. The DomainDataSource helps to communicate to Domain service and Domain service thus communicates to Data entity model. If you do not find DomainDataSource control in the toolbox, then reference it in toolbox through the DLL.

Image 9

Add the following tags in web.config file of the empty website before you proceed to bind this DomainDataSource with Gridview Control.The following tag must be added under <System.web>.

XML
<pages>
    <controls>
	<add tagPrefix="asp" namespace="Microsoft.Web.UI.WebControls" 
		assembly="Microsoft.Web.DomainServices.WebControls" />
    </controls>
</pages>

Build the solution (CTRL+F5).

6. Bind DomainDataSource with DomainService

Once we have DomainDataSource in design view, we need to bind this domain control with Domain Service Class.Configure Datasource as given in the figure below:

Image 10

Select domain service class and bind the same to DomainDatasource control. This enables insert, delete and update features.

Image 11

7. Drag GridView and Assign DomainDataSource

Drag Gridview in Empty website and configure Gridview Datasource with DomainDataSource control.Select all features like sorting, paging, editing and deleting. This will automate these features within gridview without you having to write any piece of code.

Image 12

Image 13

Now run the application (CTRl+F5). This will throw a runtime error. This is because we enabled paging and for this we need to make some changes in DomainService1.cs file. One needs to go to GetTblCustomer and comment this part of code:

C#
return this.ObjectContext.tblCustomers;

and add this:

C#
return this.ObjectContext.tblCustomers.OrderBy(p => p.CustomerID); 

This will fetch recordset as per CusotmerID and keep the page count internally. The code below is an autogenerated one and one can customize it as per the requirement.

C#
public class DomainService1 : LinqToEntitiesDomainService <AdventureEntities>
   {
      public IQueryable <tblCustomer > GetTblCustomers()
       {
           //return this.ObjectContext.tblCustomers;
           return this.ObjectContext.tblCustomers.OrderBy(p => p.CustomerID);
       }

       public void InsertTblCustomer(tblCustomer tblCustomer)
       {
           if ((tblCustomer.EntityState != EntityState.Detached))
           {
               this.ObjectContext.ObjectStateManager.ChangeObjectState
               (tblCustomer, EntityState.Added);
           }
           else
           {
               this.ObjectContext.tblCustomers.AddObject(tblCustomer);
           }
       }

Run the website and you can now update, delete, sort and navigate across recordset with WCF RIA Service feature.

Image 14

Conclusion

This demonstration is the first step towards the offering VS2010 WCF RIA and there are many features that can help developers to create wonders. Any suggestions or corrections are most welcome here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
Australia Australia
Whatsup-->Exploring--> MVC/HTML5/Javascript & Virtualization.......!
www.santoshpoojari.blogspot.com

Comments and Discussions

 
GeneralPlease fix the images... Pin
Philip Liebscher8-Jun-10 7:16
Philip Liebscher8-Jun-10 7:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.