Articles → LIGHT SWITCH 2011 → Get Data From RIA WCF Service In Lightswitch 2011
Get Data From RIA WCF Service In Lightswitch 2011
Objective Of This Tutorial
Prerequisites
- Visual studio 2010 is installed on your machine.
- Visual Studio 2010 service pack 1 is installed on your machine.
- Microsoft Visual Studio Light switch 2011 is installed on your machine.
- Silverlight runtime version 4 or 5 is installed on your machine
- WCF RIA Services SP2 for Silverlight 4 and 5 is installed on your machine.
- WCF RIA Services Toolkit is installed on your machine.
- How to create a project in Visual studio 2010?
- How to add new item in the project?
- How to add new project in the existing solution?
- Basic understanding of WCF RIA Service
- Understanding about lightswitch
Steps Involved
- Create of a new lightswitch project
- Create a WCF RIA service
- Create a domain service
- Create a database table
- Add a method to get data from table.
- Use RIA WCF service in lightswitch application
Step 1. Create A New Lightswitch Project
- Open Visual Studio
- Open the ‘New Project’ dialog box (File – New Project)
- Select ‘LightSwitch Application(Visual C#)’ in the Installed Templates.
- In the name field enter ‘Sample_App’.
- Provide location and solution name. The solution name is ‘Sample_App’ again.
Click to Enlarge
Click to Enlarge
Step 2. Create A WCF RIA Service
- Add a new project in the existing solution (File- Add - New Project).
- Go the Silverlight node in ‘Installed Templates’ (The .NET Framework should have the version of 4.0).
- Select ‘Class Library’ and provide the name and location
- In this case we have named our project as ‘MyRIAService’.
Click to Enlarge
Click to Enlarge
Step 3. Create A Domain Service
- First delete ‘Class1.cs’ from ‘MyRIAService’
- Now you must add a Domain service class to ‘MyRIAService’. For this, right click on ‘MyRIAService’ and select ‘Add New Item’.
- As shown below select ‘Domain Service Class’
Click to Enlarge
- Provide the name of the domain service as ‘MyService.cs’ and Click ‘Add’.
- A dialog box by the name of ‘Add New Domain Service Class’ will be displayed as follows:
Click to Enlarge
- Now click on ‘OK’ (Make sure that you check the ‘Enable client access ‘)
- A ‘MyRIAService’ class is generated in a new ‘MyRIAService.cs’ file as follows:
namespace MyRIAService {
using System.ComponentModel.DataAnnotations;
using System.ServiceModel.DomainServices.Hosting;
using System.ServiceModel.DomainServices.Server;
using System.Linq;
using System.Data.SqlClient;
using System.Data;
using System.Collections.Generic;
using System;
// TODO: Create methods containing your application logic.
[EnableClientAccess()]
public class MyService: DomainService {}
}
Step 4. Create A Database Table
Click to Enlarge
Click to Enlarge
Step 5. Add A Method To Get Data From Table.
public class Person { [Key]
public int PersonID {
get;
set;
}
public string PersonName {
get;
set;
}
public string PersonAddress {
get;
set;
}
}
[Query(IsDefault = true)]
public IQueryable < Person > GetPerson() {
List < Person > list = new List < Person > ();
using(SqlConnection conn = new SqlConnection("conn_string")) {
conn.Open();
using(SqlDataAdapter adap = new SqlDataAdapter("Select * from Person", conn)) {
DataSet ds = new DataSet();
adap.Fill(ds);
if (ds != null) {
foreach(DataRow row in ds.Tables[0].Rows) {
Person info = new Person();
info.PersonID = Convert.ToInt32(row["person_id"]);
info.PersonName = Convert.ToString(row["person_name"]);
info.PersonAddress = Convert.ToString(row["person_address"]);
list.Add(info);
}
}
}
}
return list.AsQueryable();
}
Step 6. Use RIA WCF Service In Lightswitch Application
- Go to lightswitch project and right click on ‘Data Sources’ folder and click on ‘Add Data Source’.
- A pop up appears to select data source type.
- From the data source type select ‘WCF RIA service’.
- Click next, a dialog box by the name of ‘Attach Data Source Wizard’ appears as shown in figure below
Click to Enlarge
- Select the service and click on ‘Next’
- Select Entity which we have added in service
Click to Enlarge
- Now click ‘Finish’.
- A warning appears which says – ‘This data source will be publicly accessible from outside your application. It is recommended that EnableClientAccess is disabled on the WCF RIA Service.’
- Click on ‘Continue’ and an service will be added in the project
Click to Enlarge
- Now right click on ‘Screens’ folder and click ‘Add Screen’.
- Select ‘Editable Grid Screen’ as screen type and select data source as the entity which we have added in RIA WCF service
Click to Enlarge
Output
Click to Enlarge