Walkthrough: Creating a RIA Services Class Library
[Note: This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

 

The WCF RIA Services class library enables you to create reusable middle-tier and presentation-tier logic. However, using the RIA Services class library is more complicated than creating a RIA Services solution. If you do not have to create reusable components or you want to see a more introductory walkthrough about creating a RIA Services solution, see Walkthrough: Creating a RIA Services Solution. For more information about the RIA Services class library, see Working with RIA Services Solutions.

In this walkthrough, you will learn how to create a Silverlight application that includes code in a RIA Services class library. 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.

Prerequisites

To create a Silverlight solution that contains a WCF RIA Services class library

  1. In Visual Studio, create a new Silverlight Application named ExampleSilverlightApp.

  2. In the New Silverlight Application dialog box, do not enable WCF RIA Services for the application.

    The Silverlight application does not need a RIA Services link between the Silverlight project and the server project because the RIA Services link will exist between the projects in the class library.

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

    The Add New Project dialog box appears.

  4. In the Silverlight category, select the WCF RIA Services Class Library template and name it AdventureWorksClassLibrary.

    Add Class Library
  5. Click OK.

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

    Class Library Solution
  6. Right-click the ExampleSilverlightApp.Web project and select Add Reference.

    The Add Reference dialog box appears.

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

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

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

To create the middle-tier library

  1. 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 RIA Services Solution.

  2. In the Entity Data Model Wizard, include the Product table in the entity model.

  3. Build the solution.

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

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

  6. Click Add.

    The Add New Domain Service Class dialog box appears.

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

  8. Build the solution.

  9. 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 System.ServiceModel.DomainServices.Client assembly.

    To find the assembly, browse to [Program Files]\Microsoft SDKs\RIA Services\v1.0\Libraries\Silverlight

  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:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  
        x:Class="ExampleSilverlightApp.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">
          <data:DataGrid Name="ProductsGrid"></data:DataGrid>
        </Grid>
    </UserControl>
    
    
    
  6. Open the code-behind for MainPage.xaml.

  7. Add the following code to retrieve the products.

    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 System.ServiceModel.DomainServices.Client;
    using AdventureWorksClassLibrary.Web;
    
    namespace RIAServicesExample
    {
        public partial class MainPage : UserControl
        {
            private ProductsDomainContext _productContext = new ProductsDomainContext();
    
            public MainPage()
            {
                InitializeComponent();
    
                LoadOperation<Product> loadOp = this._productContext.Load(this._productContext.GetProductsQuery());
                ProductsGrid.ItemsSource = loadOp.Entities;
            }
        }
    }
    
    
    
  8. Open the App.Config file in the AdventureWorksClassLibrary.Web project, and copy individually the <connectionStrings>, <system.serviceModel>, and <httpModules> 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" multipleSiteBindingsEnabled="true" />
    	</system.serviceModel>
        <system.web>
            <compilation debug="true" targetFramework="4.0" />
    	<httpModules>
        <add name="DomainServiceModule" type="System.ServiceModel.DomainServices.Hosting.DomainServiceHttpModule, System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    	</httpModules>
        </system.web>
      <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
      </system.webServer>
    
    </configuration>
    
    
    
  9. Run the application.

posted on 2010-04-19 11:38  湛然常寂  阅读(683)  评论(0编辑  收藏  举报