WCF RIA Services

Introduction

The introduction of Silverlight has paved the way for the developers working on Microsoft Technologies to develop Rich Internet Applications (RIA). The application is called RIA when the application assures the below mentioned statements.

  1. Runs on the browser.
  2. Rich user experience.
  3. Most of the presentation layer execution happens on the client side.
  4. Only the business logic and data lies on the server side.
  5. Almost equivalent to that of a desktop application kind of behavior.
  6. Most importantly does not require the installation of the software on client machine.

Silverlight is one such technology introduced by Microsoft and there are many components getting introduced to aid the developers in developing Silverlight applications. WCF RIA Service is one among them.

In this article we will learn about WCF RIA Services. You can download the WCF RIA related MSIs from Silverlight.net website.

WCF RIA Service in Silverlight Application Development

WCF RIA Services can be defined as the service which could leverage the capability of sharing the business logic/business entities between both client and server project without having to write them in both places. In specific we could take the example of the Silverlight applications. As you might be aware that the Silverlight application posses a Silverlight client and a host server project, if the developer has to share some business logic or business entity between the two project then WCF RIA Services concept lends a helping hand without which the developer may have to write the same code in both places. Fig 1.0 should explain it.

WCF RIA Services
Fig 1.0

We will be using the sample project names used in Fig 1.0 for explanation purposes in this article.

Achieving WCF RIA Service in Silverlight

In this section let us explore how the concept of WCF RIA Services can be achieved in a Silverlight application. There are two levels of creating a WCF RIA Service.

  1. Creating it in the server project (MySilverlightApplication.Web) of the Silverlight application directly.
  2. Creating a WCF RIA Services library which will enable it to be used in multiple applications.

As a first step a WCF RIA services link has to be created between the Silverlight application's host project (MySilverlightApplication.Web) and the Silverlight client project (MySilverlightApplication). There are two ways of doing it.

  1. While creating the Silverlight application check the checkbox "Enable WCF RIA Services" as shown in Fig 2.0.

    Enable WCF RIA Services
    Fig 2.0

  2. After creating the Silverlight application, right click on the project in the solution explorer and create the link by selecting the value for "WCF RIA Services Link" as shown in Fig 2.1.

    [Silverlight2.jpg]
    Fig 2.1

    In a Silverlight application, Domain Service is the one to do the trick of achieving WCF RIA Services.

Domain Service

A Domain Service created on the server side would expose the business logic/entities to the Silverlight client project through a WCF manner. The developer does not have to worry about doing the WCF things for the domain service since those will be taken care by the Microsoft Visual Studio internally. For the demonstration purpose consider the below sample application:

  1. Create a Silverlight application name MySilverlightApplication and host application would be MySilverlightApplication.Web.
  2. Add a local database with a table called Products and create an ADO.NET entity model for it. This step will create an entity class called product.
  3. Add a Domain Service in MySilverlightApplication.Web and name it as ProductDomainService. While creating the domain service you should map it with a data model and specify whether editing is allowed or not. If you say enable editing then Microsoft Visual Studio would create the Insert, Delete and Update methods for the product entity. Check it out in Fig 3.0.

     Insert, Delete and Update methods for the product entity
    Fig 3.0

You would notice a file named ProductDomainService.metadata.cs created in the server project in addition to the ProductDomainService.cs which will contain the business entities to be exposed to the client properties. You could control the properties from being exposed using the attributes like [Exclude] or [Include]. The DataAnnotation attributes like [Required], [DisplayFormat], etc can be used for defining the validation for the entity fields.

All your business logic should go into the newly created ProductDomainService.cs class. Build the MySilverlightApplication.Web and you would observe under the Silverlight client application an external folder called Generated_Code. Under this folder a file named MySilverlightApplication.Web.g.cs would show up as shown in Fig 3.1.

MySilverlightApplication.Web.g.cs
Fig 3.1

The MySilverlightApplication.Web.g.cs would expose the business entities and the acts as a portal to access the methods ingraining the business logic (ProductDomainContext class).

Accessing Data on the Silverlight Client

Until now we have learned how to share the business logic/entities between the client and server project. In this section we will learn how to consume the business logic methods in the Silverlight client project.

For accessing the business logic methods in the Silverlight client, theProductDomainContext class should be used. Below is the sample code.

  namespace MySilverlightApplication
  {
      public partial class MainPage : UserControl
      {
          ProductDomainContext _productDomainContext = new ProductDomainContext();
  
          public MainPage()
          {
              InitializeComponent();
          }
  
          private void button1_Click(object sender, RoutedEventArgs e)
          {
              EntityQuery<Product> productQuery = _productDomainContext.GetProductsQuery();
              LoadOperation<Product> loadProducts = _productDomainContext.Load(productQuery);
  
              //As the call is an asynchronous operation tie up the Completed to fecth the result back.
              loadProducts.Completed += (sender1, e1) =>
                  {
                      IEnumerable<Product> allProducts = loadProducts.Entities;
                  };          
          }
      }
  }

Since the call is an asynchronous operation a callback delegate should be defined as shown in the above code to fetch the results. Fig 4.0 shows the screenshot where the products are fetched.

 the products are fetched.
Fig 4.0

Conclusion

Hope this article delivers a good enough explanation of the WCF RIA Service and implementing a domain service in a Silverlight application. Readers comments are always greeted so do make use of the comments section to put your questions or comments or feedbacks. Happy reading!

Related Articles



About the Author

V.N.S Arun

I work for an MNC in Bangalore, India. I am fond of writing articles, posting answers in forums and submitting tips in dotnet. To contact me please feel free to make use of the "Send Email" option next to the display name.

Comments

  • WCF RIA Services Sample

    Posted by jbaurle on 06/21/2011 02:07pm

    Nice article. Thanks. Here you will find another solution using Silverlight WCF RIA Services in conjunction with SAP connectivity (ERPConnect): http://www.parago.de/2010/11/new-article-about-integrating-sap-business-data-in-silverlight-4-clients-using-wcf-ria-services-and-linq-to-sap/

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • Hundreds of millions of users have adopted public cloud storage solutions to satisfy their Private Online File Sharing and Collaboration (OFS) needs. With new headlines on cloud privacy issues appearing almost daily, the need to explore private alternatives has never been stronger. Join ESG Senior Analyst Terri McClure and Connected Data in this on-demand webinar to take a look at the business drivers behind OFS adoption, how organizations can benefit from on-premise deployments, and emerging private OFS …

Most Popular Programming Stories

More for Developers

RSS Feeds