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




  1. Visual studio 2010 is installed on your machine.
  2. Visual Studio 2010 service pack 1 is installed on your machine.
  3. Microsoft Visual Studio Light switch 2011 is installed on your machine.
  4. Silverlight runtime version 4 or 5 is installed on your machine
  5. WCF RIA Services SP2 for Silverlight 4 and 5 is installed on your machine.
  6. WCF RIA Services Toolkit is installed on your machine.




  1. How to create a project in Visual studio 2010?
  2. How to add new item in the project?
  3. How to add new project in the existing solution?
  4. Basic understanding of WCF RIA Service
  5. Understanding about lightswitch

Steps Involved




  1. Create of a new lightswitch project
  2. Create a WCF RIA service
  3. Create a domain service
  4. Create a database table
  5. Add a method to get data from table.
  6. Use RIA WCF service in lightswitch application



Step 1. Create A New Lightswitch Project




  1. Open Visual Studio
  2. Open the ‘New Project’ dialog box (File – New Project)
  3. Select ‘LightSwitch Application(Visual C#)’ in the Installed Templates.
  4. In the name field enter ‘Sample_App’.
  5. Provide location and solution name. The solution name is ‘Sample_App’ again.
Picture showing select the Lightswitch application from Visual Studio Installed templates

Click to Enlarge



Picture showing the project structure in solution explorer of the visual studio

Click to Enlarge


Step 2. Create A WCF RIA Service




  1. Add a new project in the existing solution (File- Add - New Project).
  2. Go the Silverlight node in ‘Installed Templates’ (The .NET Framework should have the version of 4.0).
  3. Select ‘Class Library’ and provide the name and location
  4. In this case we have named our project as ‘MyRIAService’.
Picture showing adding the project of type class library

Click to Enlarge



Picture showing the lightswitch and class library type project in solution explorer

Click to Enlarge


Step 3. Create A Domain Service




  1. First delete ‘Class1.cs’ from ‘MyRIAService’
  2. Now you must add a Domain service class to ‘MyRIAService’. For this, right click on ‘MyRIAService’ and select ‘Add New Item’.
  3. As shown below select ‘Domain Service Class’
  4. Picture showing adding the Domain Service Class in class library

    Click to Enlarge

  5. Provide the name of the domain service as ‘MyService.cs’ and Click ‘Add’.
  6. A dialog box by the name of ‘Add New Domain Service Class’ will be displayed as follows:
  7. Picture showing the popup window to enter the Domain Service Class details

    Click to Enlarge


  1. Now click on ‘OK’ (Make sure that you check the ‘Enable client access ‘)
  2. A ‘MyRIAService’ class is generated in a new ‘MyRIAService.cs’ file as follows:
  3. 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




Picture showing the design view of the Person table

Click to Enlarge



Picture showing the data in the Person table

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




  1. Go to lightswitch project and right click on ‘Data Sources’ folder and click on ‘Add Data Source’.
  2. A pop up appears to select data source type.
  3. From the data source type select ‘WCF RIA service’.
  4. Click next, a dialog box by the name of ‘Attach Data Source Wizard’ appears as shown in figure below
  5. Picture showing adding the RIA WCF service as data source

    Click to Enlarge



  6. Select the service and click on ‘Next’
  7. Select Entity which we have added in service
  8. Picture showing selecting the Person entity from the data source

    Click to Enlarge

  9. Now click ‘Finish’.
  10. 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.’
  11. Click on ‘Continue’ and an service will be added in the project


  12. Picture showing the RIA WCF service added as the data source in the lightswitch project

    Click to Enlarge

  13. Now right click on ‘Screens’ folder and click ‘Add Screen’.
  14. Select ‘Editable Grid Screen’ as screen type and select data source as the entity which we have added in RIA WCF service
Picture showing adding the new screen using RIA WCF service as data source

Click to Enlarge




Output


Picture showing the output of Getting the data from RIA WCF service in lightswitch 2011

Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Saturday, February 15, 2014

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250