Articles → LIGHT SWITCH 2011 → Calling custom methods in RIA WCF service

Calling custom methods in RIA WCF service






Objective of this Tutorial





Prerequisites

































Steps Involved































Step 1: Create a new lightswitch project



















Click to Enlarge


Step 2: Create a new class library project

















Click to Enlarge


Step 3. Create a Domain Service class











Click to Enlarge






Click to Enlarge







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









Click to Enlarge










Click to Enlarge




Step 6: Add a RIA service link to the Silverlight class library









Click to Enlarge




Step 7: Add a new screen to lightswitch project















Click to Enlarge


Step 8: Add a button to the screen command bar









Click to Enlarge


Step 9: Add reference in client project













Click to Enlarge


Step 10: Add a Data Source













Click to Enlarge
















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





Click to Enlarge


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

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250