Articles → LIGHT SWITCH 2011 → Calling Custom Methods In RIA WCF Service

Calling Custom Methods In RIA WCF Service






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 are 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. Understanding of WCF RIA Service
  5. Understanding about lightswitch
  6. Understanding of how to create a RIA WCF Service

Steps Involved




  1. Create a new lightswitch project
  2. Create a new class library project
  3. Create a Domain Service class
  4. Making changes to the Domain Service class
  5. Add Silverlight class library project
  6. Add a RIA service link to the Silverlight class library
  7. Add a new screen to lightswitch project
  8. Add a button to the screen command bar
  9. Add reference in client project
  10. Add a Data Source
  11. Do the necessary modifications in the LightSwitch application code
  12. Execute the application



Step 1: Create A New Lightswitch Project




  1. Open Visual Studio
  2. Create a new project (‘File’- ‘New Project’ will open the ‘New Project’ dialog box)
  3. Select ‘LightSwitchApplication(Visual C#)’ in the ‘Installed Templates’.
  4. In the ‘Name’ field enter ‘Calling_Custom_Method_In_RIA_Service’.
  5. Provide ‘Location’ and ‘Solution Name’.
  6. The ‘Solution Name’ is ‘Calling_Custom_Method_In_RIA_Service’ again.
  7. Click OK.
  8. Picture showing the New Project popup window for creating the new project of type Lightswitch Application

    Click to Enlarge

Step 2: Create A New Class Library Project




  1. Right click on the solution and click on ‘Add New Project’
  2. Under the ‘Installed Templates’, select ‘Visual C#’
  3. Select ‘Class Library’ as shown in the figure below.
  4. Provide ‘Name’ which in this case is ‘MyRIAService’.
  5. Provide ‘Location’.
  6. Click OK.
  7. Picture showing the New Project popup window for adding the new project of type class library

    Click to Enlarge

Step 3. Create A Domain Service Class




  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 new item of type Domain Service Class in the existing project

    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 first window of adding the Domain Service Class wizard to enter the class name

    Click to Enlarge

  8. Make sure that you check the ‘Enable client access ‘.
  9. Now click on ‘OK’.


namespace MyRIAService {
	using System.ServiceModel.DomainServices.Hosting;
	using System.ServiceModel.DomainServices.Server;
	using System.Linq;
	using System.Collections.Generic;
	using System.ComponentModel.DataAnnotations;

	// TODO: Create methods containing your application logic.
	[EnableClientAccess()]
	public class MyService: DomainService {}
}



Step 4: Making Changes To The Domain Service Class




namespace MyRIAService {
	using System.ServiceModel.DomainServices.Hosting;
	using System.ServiceModel.DomainServices.Server;
	using System.Linq;
	using System.Collections.Generic;
	using System.ComponentModel.DataAnnotations;

	public class Customer { [Key]
		public int CustomerID {
			get;
			set;
		}
		public string CustomerName {
			get;
			set;
		}
	}
	// TODO: Create methods containing your application logic.
	[EnableClientAccess()]
	public class MyService: DomainService { [Query(IsDefault = true)]
		public IQueryable < Customer > Get() {
			List < Customer > obj = new List < Customer > ();
			//  ... Writing logic to Get the list of all customers
			return obj.AsQueryable();
		}

		public string CustomMethod(string str) {
			return string.Format("Welcome {0}", str);
		}
	}
}





Step 5: Add Silverlight Class Library Project




  1. Open the ‘Add New Project’ window.
  2. Expand ‘Visual C#’ under the ‘Installed Templates’ and select ‘Silverlight’.
  3. Picture showing the New Project popup window for adding the new project of type silverlight class library

    Click to Enlarge

  4. Select ‘Silverlight Class Library’ from the list of templates on the right hand side.
  5. Provide ‘Name’ and ‘Location’ for the library.
  6. Click OK.
  7. A dialog box by the name of ‘Add Silverlight Class Library’ will appear as shown below
  8. Picture showing a window for selecting the lightswitch version of the silverlight class library

    Click to Enlarge

  9. Provide the version as shown above and click OK.

Step 6: Add A RIA Service Link To The Silverlight Class Library


  1. In the solution explorer, right click on the ‘Silverlight Class Library’ project (which we have just added) and click on properties.
  2. The ‘Silverlight’ tab will appear as shown below.
  3. In the tab select ‘MyRIAService’ as ‘WCF RIA Services link’.
  4. Picture showing the properties window of the silverlight class library

    Click to Enlarge

  5. Save changes and build the project to ensure no compile time error occurs

Step 7: Add A New Screen To Lightswitch Project




  1. Go the solution explorer and select the ‘Screens’ folder.
  2. Right click on ‘Screens’ folder and click ‘Add Screen…’
  3. ‘Add New Screen’ dialog box will be displayed.
  4. Select ‘Editable Grid Screen’ template.
  5. Click Ok
  6. Picture showing the window for adding the new screen of type Editable Grid Screen

    Click to Enlarge

Step 8: Add A Button To The Screen Command Bar




  1. At the top of GUI in lightswitch you will see ‘Screen Command Bar’. Right click on it and then ‘Add Button’.
  2. A button will be added to the screen command bar as shown below:
  3. Picture showing a button added in the lightswitch screen for calling the RIA WCF service

    Click to Enlarge

Step 9: Add Reference In Client Project




  1. Go to file view in the solution explorer.
  2. Right click on the ‘Client’ project and select ‘Add Reference’.
  3. Go to the ‘Projects’ tab.
  4. Select ‘MyRIAServiceModel’ project.
  5. Picture showing the window for selecting the RIA WCF service project

    Click to Enlarge

Step 10: Add A Data Source




  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 the Attach Data Source Wizard for adding the RIA WCF service

    Click to Enlarge



  6. Select the service and click on ‘Next’
  7. Select Entity which we have added in service which in our case is ‘TestEntities’
  8. Now click ‘Finish’.
  9. 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.’
  10. Click on ‘Continue’ and an service will be added in the project


Picture showing the data source added in the lightswitch application

Click to Enlarge


Step 11: Do The Necessary Modifications In The Lightswitchapplication Code




using Microsoft.LightSwitch.Presentation.Extensions;
using MyRIAService;
using System.ServiceModel.DomainServices.Client;
using Microsoft.LightSwitch.Threading;

namespace LightSwitchApplication {
	public partial class TestScreen {
		partial void CallRIACustomMethod_Execute() {
			Dispatchers.Main.BeginInvoke(() = >{
				MyContext context = new MyContext();
				InvokeOperation < string > invoke = context.CustomMethod("Gyan", OpCompleted, null);
			});
		}
		private void OpCompleted(InvokeOperation < string > invokeOp) {
			if (!invokeOp.HasError) {
				this.ShowMessageBox(invokeOp.Value);
			}
		}
	}
}



Step 12: Execute The Application




Picture showing the output of calling the custom method in RIA WCF service in lightswitch application

Click to Enlarge


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

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250