📒
OpenRiaServices
  • Start
  • Prerequisites for Open Ria Services
    • Walkthrough: Installing and Configuring SQL Server 2008 R2 Express with Advanced Services
    • Walkthrough: Installing the AdventureWorks OLTP and LT sample databases
  • Creating Open Ria Services Solutions
    • Walkthrough: Taking a Tour of Open Ria Services
    • Walkthrough: Creating a Open Ria Services Solution
    • Walkthrough: Creating a Open Ria Service with the Code First Approach
    • Walkthrough: Using the Silverlight Business Application Template
    • Walkthrough: Creating a Open Ria Services Class Library
    • Walkthrough: Localizing a Business Application
    • How to: Create a Domain Service that uses POCO-defined Entities
    • How to: Add or Remove a Open Ria Services Link
    • Using the Domain Service Wizard
  • Building Secure Applications with Open Ria Services
  • Deploying and Localizing a Open Ria Services Solutions
    • Troubleshooting the Deployment of a Open Ria Services Solution
    • Troubleshooting the Deployment of a Open Ria Services Solution
    • Walkthrough: Localizing a Business Application
  • Middle Tier
    • Domain Services
      • Walkthrough: Adding Query Methods
      • How to: Add Business Logic to the Domain Service
      • How to: Create a Domain Service that uses POCO-defined Entities
      • How to: Use HTTPS with a Domain Service
    • Data
      • Compositional Hierarchies
      • Presentation Models
      • Inheritance in Data Models
      • Complex Types
      • Shared Entities
      • Walkthrough: Sharing Entities between Multiple Domain Services
      • How to: Add Metadata Classes
      • How to: Validate Data
      • Managing Data Concurrency
    • Shared Code
      • How to: Share Code through Source Files
      • Walkthrough: Creating a Open Ria Services Class Library
  • Silverlight Clients
    • Client Code Generation
    • DomainContext and Operations
    • DomainDataSource
    • Error Handling on the Client
    • Customizing Generated Code
      • How to: Add Computed Properties on the Client
  • Accessing non-Silverlight Clients
    • ASP.NET Clients
    • Walkthrough: Using the Domain Service in ASP.NET Applications
  • Authentication, Roles, and Profiles
    • How to: Enable Authentication in Open Ria Services
    • How to: Enable Roles in Open Ria Services
    • How to: Enable Profiles in Open Ria Services
    • How to: Create a Custom Authorization Attribute
    • Walkthrough: Using Authentication Service with Silverlight Business Application
    • Walkthrough: Using Authentication Service with Silverlight Navigation Application
  • End-to-EndScenarios
    • Walkthrough: Retrieving and Displaying Data From a Domain Service
    • Walkthrough: Editing Data From a Domain Service
    • Walkthrough: Displaying Data in a Silverlight Business Application
    • Walkthrough: Displaying Related Data in a Silverlight Business Application
Powered by GitBook
On this page
  • Prerequisites
  • To create a Silverlight solution that contains a Open Ria Services class library
  • To create the middle-tier library
  • To use the generated code in the Silverlight project

Was this helpful?

  1. Creating Open Ria Services Solutions

Walkthrough: Creating a Open Ria Services Class Library

PreviousWalkthrough: Using the Silverlight Business Application TemplateNextWalkthrough: Localizing a Business Application

Last updated 4 years ago

Was this helpful?

[ This document was written for WCF Services Version 1 Service Pack 2 and might not be up to date Please see or 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 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 .

Prerequisites

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 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.

To create a Silverlight solution that contains a Open Ria Services class library

  1. Create a new Open Ria Services project in Visual Studio by selecting File, New, and then Project.

    The New Project dialog box appears.

  2. Select the Silverlight Application template from Silverlight group of the Installed Templates and name the new project ExampleSilverlightApp.

  3. 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.

  4. In Solution Explorer, right-click the solution, select Add, and then select New Project.

    The Add New Project dialog box appears.

  5. In the Silverlight category Installed Templates, select the Open Ria Services Class Library template and name it AdventureWorksClassLibrary.

  6. Click OK.

    Your solution now contains four projects as shown in the following illustration.

  7. Right-click the ExampleSilverlightApp.Web project and select Add Reference.

    The Add Reference dialog box appears.

  8. On the Projects tab, select the AdventureWorksClassLibrary.Web project and click OK.

  9. Right-click the ExampleSilverlightApp project and select Add Reference.

  10. On the Projects tab, select the AdventureWorksClassLibrary project and click OK.

To create the middle-tier library

  1. In the Entity Data Model wizard, include the Product table in the entity model.

  2. Build (Ctrl+Shift+B) the solution.

  3. Right-click the AdventureWorksClassLibrary.Web project, select Add, and then select New Item.

  4. Select the Domain Service Class template and name it ProductsDomainService.

  5. Click Add.

    The Add New Domain Service Class dialog box appears.

  6. Select Product from the available data models to expose through the domain service and click OK.

  7. Build (Ctrl+Shift+B) the solution.

  8. 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.

To use the generated code in the Silverlight project

  1. Right-click the ExampleSilverlightApp project and select Add Reference.

  2. Add a reference to the OpenRiaServices.Client assembly.

    To find the assembly, select the .NET tab.

  3. In the ExampleSilverlightApp project, open the MainPage.xaml.

  4. From the Toolbox, drag a DataGrid control to within the Grid element.

    An XML namespace and references to Data assemblies are added.

  5. Name the DataGridProductsGrid, as shown in the following XAML.

    <UserControl 
        xmlns:my="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  
        x:Class="RIAServicesExample.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        d:DesignHeight="300" d:DesignWidth="400">
    
        <Grid x:Name="LayoutRoot" Background="White">
          <my:DataGrid Name="ProductsGrid"></my:DataGrid>
        </Grid>
    </UserControl>
  6. Open the code-behind for MainPage.xaml.

  7. Add the following code to retrieve the products.

    Imports System.Windows.Ria
    Imports RIAServicesExample.Web
    
    Partial Public Class MainPage
        Inherits UserControl
    
        Private _productContext As New ProductDomainContext
    
        Public Sub New()
            InitializeComponent()
    
            Dim loadOp = Me._productContext.Load(Me._productContext.GetProductsQuery())
            ProductGrid.ItemsSource = loadOp.Entities
        End Sub
    
    End Class
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using RIAServicesExample.Web;
    using System.Windows.Ria;
    
    namespace RIAServicesExample
    {
        public partial class MainPage : UserControl
        {
            private ProductDomainContext _productContext = new ProductDomainContext();
    
            public MainPage()
            {
                InitializeComponent();
    
                LoadOperation<Customer> loadOp = this._productContext.Load(this._productContext.GetProductsQuery());
                CustomerGrid.ItemsSource = loadOp.Entities;
            }
        }
    }
  8. 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.

    <configuration>
        <connectionStrings>
            <add name="AdventureWorksLT2008Entities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=example;Initial Catalog=AdventureWorksLT2008;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
        </connectionStrings>
        <system.serviceModel>
            <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
        </system.serviceModel>
        <system.web>
            <compilation debug="true" targetFramework="4.0" />
        <httpModules>
            <add name="DomainServiceModule" type="System.Web.Ria.Services.DomainServiceHttpModule, System.Web.Ria, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        </httpModules>
        </system.web>
        <system.webServer>
          <modules runAllManagedModulesForAllRequests="true"/>
        </system.webServer>
    
    </configuration>
  9. Run (F5) the application.

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
Release Notes
Changelog
Creating Open Ria Services Solutions
Walkthrough: Creating a Open Ria Services Solution
Prerequisites for Open Ria Services
RIA_ClassLibraryProjects
RIA_AddClassLibrary