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
To create a Silverlight solution that contains a WCF Ria services class library
-
In Visual Studio, create a new Silverlight application namedExamplesilverlightapp.
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.
-
In Solution Explorer, right-click the solution, selectAdd, And then selectNew project.
TheAdd new projectDialog box appears.
-
In the Silverlight category, selectWCF Ria services class libraryTemplate and name itAdventureworksclasslibrary.
-
ClickOK.
Your solution now contains four projects as shown in the following practice.
Right-click the examplesilverlightapp. Web project and selectAdd reference.
TheAdd referenceDialog box appears.
-
OnProjectsTab, selectAdventureworksclasslibrary. WebProject and clickOK.
-
Right-click the examplesilverlightapp project and selectAdd reference.
-
OnProjectsTab, selectAdventureworksclasslibraryProject and clickOK.
To create the Middle-tier Library
In the adventureworksclasslibrary. Web project, addADO. NET Entity Data ModelNamedAdventureworksmodel. edmx. For steps on how to do this, seeWalkthrough: creating a RIA services Solution.
-
In the Entity Data Model Wizard, includeProductTable in the entity model.
-
Build the solution.
-
Right-click the adventureworksclasslibrary. Web project, selectAdd, And then selectNew item.
-
SelectDomain service classTemplate and name itProductsdomainservice.
-
ClickAdd.
TheAdd new domain service classDialog box appears.
SelectProductFrom the available data models to expose through the domain service and clickOK.
-
Build the solution.
-
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
-
Right-click the examplesilverlightapp project and selectAdd reference.
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
-
In the examplesilverlightapp project, open the mainpage. XAML.
-
From the toolbox, dragDataGridControl to withinGridElement.
An XML namespace and references to data assemblies are added.
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 >
-
Open the code-behind for mainpage. XAML.
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 ;}}}
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>
-
Run the application.