Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

WCF RIA Services: ASP.NET Client Web Application

0.00/5 (No votes)
1 Jun 2010 1  
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

Add three references of WCF RIA Services:

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

2. Create Data Model

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.

Build the solution (CTRL+Shift+B).

3. Create Domain Service

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

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.

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>.

<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:

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

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.

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:

return this.ObjectContext.tblCustomers;

and add this:

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.

 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.

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here