Articles → LIGHT SWITCH 2011 → Calling Custom Methods In RIA WCF Service
Calling Custom Methods In RIA WCF Service
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 are 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?
- Understanding of WCF RIA Service
- Understanding about lightswitch
- Understanding of how to create a RIA WCF Service
Steps Involved
- Create a new lightswitch project
- Create a new class library project
- Create a Domain Service class
- Making changes to the Domain Service class
- Add Silverlight class library project
- Add a RIA service link to the Silverlight class library
- Add a new screen to lightswitch project
- Add a button to the screen command bar
- Add reference in client project
- Add a Data Source
- Do the necessary modifications in the LightSwitch application code
- Execute the application
Step 1: Create A New Lightswitch Project
- Open Visual Studio
- Create a new project (‘File’- ‘New Project’ will open the ‘New Project’ dialog box)
- Select ‘LightSwitchApplication(Visual C#)’ in the ‘Installed Templates’.
- In the ‘Name’ field enter ‘Calling_Custom_Method_In_RIA_Service’.
- Provide ‘Location’ and ‘Solution Name’.
- The ‘Solution Name’ is ‘Calling_Custom_Method_In_RIA_Service’ again.
- Click OK.
Click to Enlarge
Step 2: Create A New Class Library Project
- Right click on the solution and click on ‘Add New Project’
- Under the ‘Installed Templates’, select ‘Visual C#’
- Select ‘Class Library’ as shown in the figure below.
- Provide ‘Name’ which in this case is ‘MyRIAService’.
- Provide ‘Location’.
- Click OK.
Click to Enlarge
Step 3. Create A Domain Service Class
- 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
- Make sure that you check the ‘Enable client access ‘.
- 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
- Open the ‘Add New Project’ window.
- Expand ‘Visual C#’ under the ‘Installed Templates’ and select ‘Silverlight’.
Click to Enlarge
- Select ‘Silverlight Class Library’ from the list of templates on the right hand side.
- Provide ‘Name’ and ‘Location’ for the library.
- Click OK.
- A dialog box by the name of ‘Add Silverlight Class Library’ will appear as shown below
Click to Enlarge
- Provide the version as shown above and click OK.
Step 6: Add A RIA Service Link To The Silverlight Class Library
- In the solution explorer, right click on the ‘Silverlight Class Library’ project (which we have just added) and click on properties.
- The ‘Silverlight’ tab will appear as shown below.
- In the tab select ‘MyRIAService’ as ‘WCF RIA Services link’.
Click to Enlarge
- Save changes and build the project to ensure no compile time error occurs
Step 7: Add A New Screen To Lightswitch Project
- Go the solution explorer and select the ‘Screens’ folder.
- Right click on ‘Screens’ folder and click ‘Add Screen…’
- ‘Add New Screen’ dialog box will be displayed.
- Select ‘Editable Grid Screen’ template.
- Click Ok
Click to Enlarge
Step 8: Add A Button To The Screen Command Bar
- At the top of GUI in lightswitch you will see ‘Screen Command Bar’. Right click on it and then ‘Add Button’.
- A button will be added to the screen command bar as shown below:
Click to Enlarge
Step 9: Add Reference In Client Project
- Go to file view in the solution explorer.
- Right click on the ‘Client’ project and select ‘Add Reference’.
- Go to the ‘Projects’ tab.
- Select ‘MyRIAServiceModel’ project.
Click to Enlarge
Step 10: Add A Data Source
- 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 which in our case is ‘TestEntities’
- 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
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