zoukankan      html  css  js  c++  java
  • 转载:如何使用Silverlight+WCF Ria Services Class Library 类库

    如何使用Silverlight+WCF Ria Services Class Library 类库

    1. 打开VS2010新建一个项目,类型选择Silverlight Application

    2. 第二步勾上“Enable WCF Ria Service”,会给你自动加上引用一些类库

    2011-05-12 10h35_58

    3. 拖一个DataGrid控件到MainPage.xaml里面,设置属性 AutoGenerateColumns="True"

    4. 添加数据模型 DomainLayer,这里用Entity Framework来做数据模型,自动获得ORM和持久化。方法是右键点击你的solution,添加一个class library项目,然后在这个项目中添加新项:Ado.NET Entity Data Model

    2011-05-12 10h42_31

    5. 会要求连接到数据库,选择 Generate from database,选择Connection String,选择表和视图等,然后生成edmx。

    6. 生成数据模型以后,build solution

    7. 右键点击你的solution,添加新的项目,类型选择 WCF Ria Service Class Library

    2011-05-12 10h46_29

    8. 会自动生成两个项目 RIAServicesLibrary1和RIAServicesLibrary1.web,前者是客户端的Silverlight项目,后者是服务器端的项目,两者有共享代码。

    9. 添加引用:SilverlightApplication1项目添加对RIAServicesLibrary1项目的引用,SilverlightApplication1.Web添加对RIAServicesLibrary1.Web的引用。

    10. RIAServicesLibrary1.Web项目添加对数据模型 DomainLayer项目(第4步添加的项目)的引用。

    11. RIAServicesLibrary1.Web项目添加新项:Domain Service Class

    2011-05-12 10h51_23

    12. 会出来一个界面选择DataContext,选择你需要的即可。

    2011-05-12 10h53_05

    13. 生成

    14. RIAServicesLibrary1项目可以按需添加调用RIAServicesLibrary1.Web的Domain service,例如:

    调用DomainServices

    1 using System;
    2 using System.Collections.Generic;
    3 using System.Net;
    4 using System.Windows;
    5 using System.Windows.Controls;
    6 using System.Windows.Documents;
    7 using System.Windows.Ink;
    8 using System.Windows.Input;
    9 using System.Windows.Media;
    10 using System.Windows.Media.Animation;
    11 using System.Windows.Shapes;
    12
    13 using RIAServicesLibrary1.Web;
    14 using System.ServiceModel.DomainServices.Client;
    15 using DataModel;
    16
    17 namespace RIAServicesLibrary1
    18 {
    19 public class TestClass
    20 {
    21 public IEnumerable<Company> GetCompanyList()
    22 {
    23 DomainService1 s = new DomainService1();
    24 LoadOperation<Company> loadOp = s.Load(s.GetCompaniesQuery());
    25 return loadOp.Entities;
    26 }
    27 }
    28 }

    15. 前台调用RIAServicesLibrary1的代码并显示,例如:

    MainPage.xaml.cs

    1 using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Net;
    5 using System.Windows;
    6 using System.Windows.Controls;
    7 using System.Windows.Documents;
    8 using System.Windows.Input;
    9 using System.Windows.Media;
    10 using System.Windows.Media.Animation;
    11 using System.Windows.Shapes;
    12 using RIAServicesLibrary1;
    13
    14 namespace RiaClassLibraryTest
    15 {
    16 public partial class MainPage : UserControl
    17 {
    18 public MainPage()
    19 {
    20 InitializeComponent();
    21
    22 TestClass c = new TestClass();
    23 dataGrid1.ItemsSource = c.GetCompanyList();
    24 }
    25 }
    26 }

    16. 调整项目结构,按Client,Server进行组合,例如:

    2011-05-12 10h58_08

    17. 修改Web.config,否则会遇到一大堆错误(遇到错误请用Fiddler进行调试)

    Web.config

    1 <?xml version="1.0" encoding="utf-8"?>
    2 <!--
    3 For more information on how to configure your ASP.NET application, please visit
    4 http://go.microsoft.com/fwlink/?LinkId=169433
    5 -->
    6 <configuration>
    7 <system.webServer>
    8 <modules runAllManagedModulesForAllRequests="true">
    9 <add name="DomainServiceModule" preCondition="managedHandler"
    10 type="System.ServiceModel.DomainServices.Hosting.DomainServiceHttpModule, System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    11 </modules>
    12 <validation validateIntegratedModeConfiguration="false" />
    13 </system.webServer>
    14 <system.web>
    15 <httpModules>
    16 <add name="DomainServiceModule" type="System.ServiceModel.DomainServices.Hosting.DomainServiceHttpModule, System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    17 </httpModules>
    18 <compilation debug="true" targetFramework="4.0">
    19 <assemblies>
    20 <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    21 </assemblies>
    22 </compilation>
    23 </system.web>
    24 <connectionStrings>
    25 <add name="MyEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=192.168.1.345;initial catalog=Northwind;persist security info=True;user id=sa;password=******;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    26 </connectionStrings>
    27 <system.serviceModel>
    28 <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
    29 multipleSiteBindingsEnabled="true" />
    30 </system.serviceModel>
    31 </configuration>

    18. 运行。(注:System.ServiceModel.DomainServices.Client.dll 位于 C:\Program Files\Microsoft SDKs\RIA Services\v1.0\Libraries\Silverlight 目录下。)

    继续阅读:WCF Ria Services Class Library类库的进阶(下一篇)

    转载地址:http://www.cnblogs.com/Mainz/archive/2011/05/12/2044140.html

  • 相关阅读:
    word设置的密码忘了怎么办?
    Navicat Report Viewer 设置 HTTP 的方法
    如何处理Navicat Report Viewer 报表
    excel密码忘记了怎么办
    Beyond Compare文本比较搜索功能详解
    Popular Cows POJ
    Problem B. Harvest of Apples HDU
    网络流模型整理
    The Shortest Statement CodeForces
    Vasya and Multisets CodeForces
  • 原文地址:https://www.cnblogs.com/finehappy/p/2068443.html
Copyright © 2011-2022 走看看