WCF RIA Services 客户端、服务端的处理方法和例子

简介: Silverlight客户端访问 1. 首先Project Link到服务端项目,Build服务端项目是在客户端项目的Generated_Code和其他一些目录下会生成相关的代码 2. 使用代码或XAML(DomainDataSource )访问服务 xmlns:domain="clr-namespace:RIA.

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?$where=(it.City%253d%253d%2522london%2522)&$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 项目

运行效果如下图:

clip_image002

包括了数据的列表显示,数据的编辑和Domain Service方法,具体有:

DomainDataSource 的使用:过滤、分页、排序、分组等

DataForm 和DomainDataSource结合实现数据的增删改

手工的调用服务端方法

Sliverlight中,如果对于比较大的应用,为了更清晰的分层,可以考虑使用MVVP模式

特别注意:Sliverlight中调用服务端的代码,很多情况下都是异步的[具体的原因和浏览器有关,详细解释参考MSDN],因此使用方法返回的结果需要注意

相关文章
|
9月前
|
Windows
WCF服务端调用客户端.
WCF服务端调用客户端.
|
9月前
|
Windows
|
数据库 测试技术 安全
使用Entity Framework和WCF Ria Services开发SilverLight之2:POCO
在上一篇中《使用Entity Framework和WCF Ria Services开发SilverLight之1:简单模型》我们提出这类简单模型的几个问题: 1:实体模型被紧耦合在EDM中,同时它不能项目(模块)使用。
1221 0
|
监控
Wcf通讯基础框架方案(三)——客户端
假设定义了一个服务契约: [ServiceContract(Namespace = "WcfExtension.Services.Interface")] public interface ITestService { [OperationContract] ...
700 0
|
前端开发
WCF更新服务引用报错的原因之一
WCF更新服务引用报错的原因之一
|
11月前
|
C# 数据安全/隐私保护
c#如何创建WCF服务到发布(SqlServer版已经验证)
c#如何创建WCF服务到发布(SqlServer版已经验证)
47 0
|
11月前
|
安全 数据库连接 数据库
WCF服务创建到发布(SqlServer版)
在本示例开始之前,让我们先来了解一下什么是wcf? wcf有哪些特点? wcf是一个面向服务编程的综合分层架构。该架构的项层为服务模型层。 使用户用最少的时间和精力建立自己的软件产品和外界通信的模型。它使得开发者能够建立一个跨平台的安全、可信赖、事务性的解决方案。且能与已有系统兼容写作。 简单概括就是:一组数据通信的应用程序开发接口。
83 0