Creating a RIA services class library

Source: Internet
Author: User
Tags visual studio 2010
SELF: http://msdn.microsoft.com/en-us/library/ee707351 (vs.91 ). aspxwalkthrough: creating a RIA services Class Library [Note: this topic is pre-release documentation and is subject to change in future releases. blank topics are supported 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, seeWalkthrough: creating a RIA services Solution. For more information about the RIA services class library, seeWorking with RIA services solutions.

In this walkthrough, you will learn how to create a Silverlight application that includes des 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

    • Visual Studio 2010 RC

    • Silverlight 4 RC

    • WCF Ria services RC for Visual Studio 2010

    • Adventureworkslt Sample Database

To create a Silverlight solution that contains a WCF Ria services class library
  1. In Visual Studio, create a new Silverlight application namedExamplesilverlightapp.

  2. InNew Silverlight ApplicationDialog 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, selectAdd, And then selectNew project.

    TheAdd new projectDialog box appears.

  4. In the Silverlight category, selectWCF Ria services class libraryTemplate and name itAdventureworksclasslibrary.

  5. ClickOK.

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

  6. Right-click the examplesilverlightapp. Web project and selectAdd reference.

    TheAdd referenceDialog box appears.

  7. OnProjectsTab, selectAdventureworksclasslibrary. WebProject and clickOK.

  8. Right-click the examplesilverlightapp project and selectAdd reference.

  9. OnProjectsTab, selectAdventureworksclasslibraryProject and clickOK.

To create the Middle-tier Library
  1. In the adventureworksclasslibrary. Web project, addADO. NET Entity Data ModelNamedAdventureworksmodel. edmx. For steps on how to do this, seeWalkthrough: creating a RIA services Solution.

  2. In the Entity Data Model Wizard, includeProductTable in the entity model.

  3. Build the solution.

  4. Right-click the adventureworksclasslibrary. Web project, selectAdd, And then selectNew item.

  5. SelectDomain service classTemplate and name itProductsdomainservice.

  6. ClickAdd.

    TheAdd new domain service classDialog box appears.

  7. SelectProductFrom the available data models to expose through the domain service and clickOK.

  8. Build the solution.

  9. In Solution Explorer, selectShow all filesIn 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 selectAdd 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, dragDataGridControl to withinGridElement.

    An XML namespace and references to data assemblies are added.

  5. NameDataGridProductsgrid, As shown in the following XAML.

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

    VB C # C ++ F # jscriptcopy
     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<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.

    VB C # C ++ F # jscriptcopy
    <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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.