The purpose of this tutorial is to show you how to make a call to an .NET RIA Service in the context of a Silverlight application. The following common tasks will be examined:
Creating a new instance of the RIA Service context
Consuming the result.
The process of developing a .NET RIA Service is beyond the scope of this tutorial. For more information read here.
This tutorial will use the following product and technology:
Northwind database, which can be downloaded from here.
ADO.NET Entity Framework - used to create an Entity Data Model representation of the Northwind database.
When using .NET RIA Services there is no need to add reference to the server project, as you should do with the other types of services for example. The DomainService class is available on the client thanks to the code, generated by the server in the GeneratedCode folder. This is one of the features of the .NET RIA Services Framework - it allows to create a link between the server and the client.
The GeneratedCode is not included in the project, that's why you should enable the Show All Files button in the Solution Explorer.
SampleRiaContext riaContext = new SampleRiaContext();
Dim riaContext As New SampleRiaContext()
The SampleRiaContext represents your DomainService.
In Silverlight all service calls are performed asynchronously. In order to make an asynchronous call to your service you need to do the following steps:
Create a new EntityQuery.
Load the data using the created EntityQuery.
Attach to the event fired when the loaded operation completes.
Here is a sample code showing how this can be achieved:
LoadOperation<Categories> loadOperation = riaContext.Load<Categories>( riaContext.GetCategoriesQuery() );
loadOperation.Completed += new EventHandler( loadOperation_Completed );
Dim loadOperation As LoadOperation(Of Categories) = riaContext.Load(Of Categories)(riaContext.GetCategoriesQuery())
AddHandler loadOperation.Completed, AddressOf loadOperation_Completed
Once the load operation is completed accessing the result is an extremely simple process:
private void loadOperation_Completed( object sender, EventArgs e )
//Consume the result
List<Categories> result = riaContext.Categories.ToList();
Private Sub loadOperation_Completed(ByVal sender As Object, ByVal e As EventArgs)
'Consume the result'
Dim result As List(Of Categories) = riaContext.Categories.ToList()