WCF RIA Services 客户端、服务端的处理方法和例子
Silverlight客户端访问
1. 首先Project Link到服务端项目,Build服务端项目是在客户端项目的Generated_Code和其他一些目录下会生成相关的代码
2. 使用代码或XAML(DomainDataSource )访问服务
xmlns:domain="clr-namespace:RIA.Web.Services"
Title="Demo Page" Style="{StaticResource PageStyle}"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" xmlns:riaControls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.DomainServices">
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<riaControls:DomainDataSource Name="DataSource" AutoLoad="True"
QueryName="GetCustomers">
<riaControls:DomainDataSource.DomainContext>
<domain:NorthWindContext/>
</riaControls:DomainDataSource.DomainContext>
</riaControls:DomainDataSource>
<sdk:DataGrid Name="dataGrid1" Grid.Row="0" ItemsSource="{Binding ElementName=DataSource, Path=Data}" />
<sdk:DataGrid Name="dataGrid2" Grid.Row="1"/>
</Grid>
NorthWindContext context = new NorthWindContext();
EntityQuery<Customer> query = context.GetCustomersQuery();
///Load all
//context.Load<Customer>(query);
//this.dataGrid1.ItemsSource = context.Customers;
var q = from c in query where c.City == "london" orderby c.CustomerID select c;
context.Load<Customer>(q);
this.dataGrid2.ItemsSource = context.Customers;
查看HTTP协议可以看到发到服务端的请求如下:
GET /ClientBin/RIA-Web-Services-NorthWindService.svc/binary/GetCustomers
GET /ClientBin/RIA-Web-Services-NorthWindService.svc/binary/GetCustomers?orderby=it.CustomerID HTTP/1.1
可以看到整个的方式和WCF Data Service基本一致
注:DomainDataSource包含在 System.Windows.Controls.DomainServices组件中
服务端Domain Service方法
服务端服务类中的方法遵循如下的约定
方法前缀 |
标注属性 |
说明 |
(Any) |
[Query()] |
A method that returns data without any side effects. The usual approach is to prefix with Get, but any prefix is fine as long as the function returns an instance of an entity T, an IEnumerable<T>, or an IQueryable<T>. |
Insert, Add, Create |
[Insert()] |
An operation that inserts a single entity into the data store. The method takes the entity as a parameter. |
Update, Change, Modify |
[Update()] |
An operation that updates a single entity in the data store. The method takes the entity as a parameter. |
Delete, Remove |
[Delete()] |
An operation that deletes an entity in the data store. The method takes the entity as a parameter. |
(Any) |
[Invoke()] |
A business method that must be executed without tracking or deferred execution. It may or may not have side effects. Use only when one of the other method types can’t be used. |
(Any) |
[Update(UsingCustomMethod=true)] |
标注UsingCustomMethod=true属性,执行特定更新的操作,如需要手动进行级联更新的情况 参数是实体类,在SubmitChanges调用时,自动调用这个更新实体的函数 |
- |
[Ignore()] |
方法尽管符合约定如UpdateEmployee,但不要生成客户端的桩 |
以上的说明都是在服务端类中可以编写的方法,在这个类中你可以进行业务逻辑的验证和处理,数据的更新等等各种操作.
例子
http://dskit.codeplex.com 上的RIA RIA.Web 项目
运行效果如下图:
包括了数据的列表显示,数据的编辑和Domain Service方法,具体有:
DomainDataSource 的使用:过滤、分页、排序、分组等
DataForm 和DomainDataSource结合实现数据的增删改
手工的调用服务端方法
Sliverlight中,如果对于比较大的应用,为了更清晰的分层,可以考虑使用MVVP模式
特别注意:Sliverlight中调用服务端的代码,很多情况下都是异步的[具体的原因和浏览器有关,详细解释参考MSDN],因此使用方法返回的结果需要注意
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南