Walkthrough: Creating a Open Ria Services Class Library
Last updated
Last updated
[ This document was written for WCF Services Version 1 Service Pack 2 and might not be up to date Please see Release Notes or Changelog for a list of changes since WCF RIA Services ]
In this walkthrough, you learn how to create a Silverlight application that includes reusable components in a Open Ria Services class library.
The Open Ria Services class library enables you to create reusable middle-tier and presentation-tier logic. However, using the Open Ria Services class library is more complicated than creating a Open Ria Services solution.
To simplify the example, this walkthrough shows the class library in the same solution as the Silverlight application. The class library can exist in a separate solution.
For more some background on Open Ria Services class libraries, see the relevant sections of the Creating Open Ria Services Solutions topic.
Note: If you do not need to create reusable components or if you want to see a more introductory walkthrough about creating a Open Ria Services solution, see Walkthrough: Creating a Open Ria Services Solution.
This and the other walkthroughs presented in the Open Ria Services documentation require several prerequisite programs, such as Visual Studio and the Silverlight Developer Runtime and SDK, be installed and configured properly, in addition to Open Ria Services and the Open Ria Services Toolkit. They also require installing and configuring SQL Server 2008 R2 Express with Advanced Services and installing the AdventureWorks OLTP and LT database.
Detailed instructions for the satisfaction of each of these prerequisites are provided by the topics within the Prerequisites for Open Ria Services node. Follow the instructions provided there before proceeding with this walkthrough to ensure that you encounter as few problems as possible when working through this Open Ria Services walkthroughs.
Create a new Open Ria Services project in Visual Studio by selecting File, New, and then Project.
The New Project dialog box appears.
Select the Silverlight Application template from Silverlight group of the Installed Templates and name the new project ExampleSilverlightApp.
In the New Silverlight Application dialog box, do not check the Enable Open Ria Services box for the application.
The Silverlight application does not need a Open Ria Services link between the Silverlight project and the server project because the Open Ria Services link will exist between the projects in the class library.
In Solution Explorer, right-click the solution, select Add, and then select New Project.
The Add New Project dialog box appears.
In the Silverlight category Installed Templates, select the Open Ria Services Class Library template and name it AdventureWorksClassLibrary.
Click OK.
Your solution now contains four projects as shown in the following illustration.
Right-click the ExampleSilverlightApp.Web project and select Add Reference.
The Add Reference dialog box appears.
On the Projects tab, select the AdventureWorksClassLibrary.Web project and click OK.
Right-click the ExampleSilverlightApp project and select Add Reference.
On the Projects tab, select the AdventureWorksClassLibrary project and click OK.
In the AdventureWorksClassLibrary.Web project, add an ADO.NET Entity Data Model named AdventureWorksModel.edmx. For steps on how to do this, see Walkthrough: Creating a Open Ria Services Solution.
In the Entity Data Model wizard, include the Product table in the entity model.
Build (Ctrl+Shift+B) the solution.
Right-click the AdventureWorksClassLibrary.Web project, select Add, and then select New Item.
Select the Domain Service Class template and name it ProductsDomainService.
Click Add.
The Add New Domain Service Class dialog box appears.
Select Product from the available data models to expose through the domain service and click OK.
Build (Ctrl+Shift+B) the solution.
In Solution Explorer, select Show All Files in each project.
Notice the Generated_Code folder only exists in the AdventureWorksClassLibrary project. Although no code was generated for the ExampleSilverlightApp project, you can still use the generated code from the AdventureWorksClassLibrary project because a project reference exists between the ExampleSilverlightApp and AdventureWorksClassLibrary projects.
Right-click the ExampleSilverlightApp project and select Add Reference.
Add a reference to the OpenRiaServices.Client assembly.
To find the assembly, select the .NET tab.
In the ExampleSilverlightApp project, open the MainPage.xaml.
From the Toolbox, drag a DataGrid control to within the Grid element.
An XML namespace and references to Data assemblies are added.
Name the DataGridProductsGrid, as shown in the following XAML.
Open the code-behind for MainPage.xaml.
Add the following code to retrieve the products.
Open the App.Config file in the AdventureWorksClassLibrary.Web project, and copy individually the \, \, and \ elements and the elements they contain. Paste each element individually into the Web.config file of the ExampleSilverlightApp.Web project. The Web.config file will look similar to the following example, but your file must provide the relevant connection information for your environment.
Run (F5) the application.