如何使用Silverlight+WCF Ria Services Class Library 类库
1. 打开VS2010新建一个项目,类型选择Silverlight Application
2. 第二步勾上“Enable WCF Ria Service”,会给你自动加上引用一些类库
3. 拖一个DataGrid控件到MainPage.xaml里面,设置属性 AutoGenerateColumns="True"
4. 添加数据模型 DomainLayer,这里用Entity Framework来做数据模型,自动获得ORM和持久化。方法是右键点击你的solution,添加一个class library项目,然后在这个项目中添加新项:Ado.NET Entity Data Model
5. 会要求连接到数据库,选择 Generate from database,选择Connection String,选择表和视图等,然后生成edmx。
6. 生成数据模型以后,build solution
7. 右键点击你的solution,添加新的项目,类型选择 WCF Ria Service Class Library
8. 会自动生成两个项目 RIAServicesLibrary1和RIAServicesLibrary1.web,前者是客户端的Silverlight项目,后者是服务器端的项目,两者有共享代码。
9. 添加引用:SilverlightApplication1项目添加对RIAServicesLibrary1项目的引用,SilverlightApplication1.Web添加对RIAServicesLibrary1.Web的引用。
10. RIAServicesLibrary1.Web项目添加对数据模型 DomainLayer项目(第4步添加的项目)的引用。
11. RIAServicesLibrary1.Web项目添加新项:Domain Service Class
12. 会出来一个界面选择DataContext,选择你需要的即可。
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进行组合,例如:
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="data source=192.168.1.345;initial catalog=Northwind;persist security info=True;user id=sa;password=******;multipleactiveresultsets=True;App=EntityFramework"" 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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?