How to Create a WCF RIA Service for a LightSwitch Application

Prologue:

In this article we shall see how to create a WCF RIA Service for a LightSwitch Application.

WCF RIA Services simplifies the development of n-tier solutions for Rich Internet Applications (RIA), such as Silverlight and LightSwitch applications.

WCFRIAServiceforLightSwitch1.jpg
               
The above figure exposes the WCF RIA Service nature.

Preparing Solution:

Start the VS 2010 and select Silverlight from Installed Templates. From this select WCF RIA Services Class Library and name it LightSwitchRIAService.

WCFRIAServiceforLightSwitch2.jpg
 
In the above figure, the rounded item is to be selected for creating RIA Services. After adding the RIA project your solution will have two Projects as shown in the picture below. 

WCFRIAServiceforLightSwitch3.jpg
                                        
Adding Entity Data Model:

WCFRIAServiceforLightSwitch4.jpg
 
The Entity Data Model is nothing but the Middle Tier for our LightSwitch Application.

To add Entity Model, select Add New Item by right clicking the Project.

Setting Connection Properties:

An Entity Data Model Wizard will be displayed after clicking the Add  button. 

Before setting the connection properties you should have a database with a table with a primary key column.

WCFRIAServiceforLightSwitch5.jpg 

In this wizard click the "New Connection" button if a relevant connection is not available in the combo box.

Give the ConnectionString the name PersonEntity as shown in the above figure.

Click Next to continue.

Select your Table:

The next screen will guide you to select the Tables from the Database which you have selected in the previous screen.

WCFRIAServiceforLightSwitch6.jpg

Select a table to be bonded with your LightSwitch Application Data Source.

That's it; the Entity Data Model is now ready. The Next step is to create the DomainService.

Creating DomainService:

To create the DomainService Class, right-click the LightSwitchRIAService.Web project and add a new item.

It will display a window like below.

WCFRIAServiceforLightSwitch10.jpg
 
As per the figure, select Web from the Installed Templates and select the Domain Service Class.

It will display the following window.

WCFRIAServiceforLightSwitch11.jpg
 
From this window select PersonEntities that we have created and then select the Person.

After following all these steps, finally ensure that you have created the Entity Model by seeing an Entity Diagram as like the following one.

WCFRIAServiceforLightSwitch12.jpg
                                         
Build the Solution to check whether everything is done correctly so far.

Important point to Note:
  1. Add an Attribute to a GetPeople () Method of PersonDomainService.cs as shown in the following figure. By default it won't be there.

    WCFRIAServiceforLightSwitch13.jpg

  2. Add a Key Attribute to the Primary Key entity in PersonDomainService.metadata.cs as shown in the figure.

    WCFRIAServiceforLightSwitch14.jpg
Build the Solution again. Now the RIA Service is ready for LightSwitch to consume the Data.

Summary:

In this article, we have seen the basic things to create a WCF RIA Service for LightSwitch Application.

Thanks for spending your precious time here. Please provide your valuable feedbacks and comments, which enable me to give a better article the next time. 

Up Next
    Ebook Download
    View all
    Learn
    View all