RIA Service 的 SOAP EndPoint
原文 www.cyqdata.com/cnblogs/article-detail-39983-english
越来越多的朋友都在使用Silverlight开发应用程序,其中我们常用的还会有一个特殊的服务,就是RIA Service,具体来说,它是一个所谓的Domain Service. 关于这个服务的基本概念,如果有不清楚,请参考下面这个网址
http://www.silverlight.net/getstarted/riaservices/
今天这一篇是要谈谈一个问题,就是如何让RIA Service支持WPF此类客户端来访问?
你可能会疑惑,这难道是一个问题吗?是的,RIA Service默认只能在Silverlight里面访问到。我们还是通过一个例子来讲解一下吧
1. 定义一个业务实体类
namespace WebApplication1
{
using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;
using System.ServiceModel.DomainServices.Server; [DataContract]
public class Employee
{
[Key][DataMember]
public int ID { get { return this.GetHashCode(); } } [DataMember]
public string FirstName { get; set; }
[DataMember]
public string LastName { get; set; }
}
}
2. 创建一个Domain Service
namespace WebApplication1
{
using System.Linq;
using System.ServiceModel.DomainServices.Hosting;
using System.ServiceModel.DomainServices.Server; // TODO: Create methods containing your application logic.
[EnableClientAccess()]
public class SampleDomainService : DomainService
{
[Query]
public IQueryable<Employee> GetEmployees()
{
return new[]{
new Employee{FirstName="ares",LastName="chen"},
new Employee{FirstName="mike",LastName="fei"},
new Employee{FirstName="tom",LastName="jerry"}
}.AsQueryable();
}
}
}
【备注】需要注意的是,Domain Service是没有svc文件的,这与标准的WCF服务是完全不同的。

3. 在Silverlight应用程序中访问这个服务
作为演示,我们做了一个很简单的界面
<UserControl x:Class="SilverlightApplication1.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" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"> <Grid x:Name="LayoutRoot" Background="White">
<sdk:DataGrid AutoGenerateColumns="True" Margin="24,53,21,25" Name="dataGrid1" />
<TextBlock Height="33" HorizontalAlignment="Left" Margin="25,14,0,0" Name="textBlock1" Text="Employee List" VerticalAlignment="Top" Width="219" FontSize="25" />
</Grid>
</UserControl>
然后通过如下的代码调用数据
using System.Windows;
using System.Windows.Controls; namespace SilverlightApplication1
{ using WebApplication1; public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent(); Loaded += new RoutedEventHandler(MainPage_Loaded);
} void MainPage_Loaded(object sender, RoutedEventArgs e)
{
var ctx = new SampleDomainContext();
var op = ctx.Load<Employee>(ctx.GetEmployeesQuery());
dataGrid1.ItemsSource = op.Entities;
}
}
}
从上面的代码不难看出,在Silverlight调用RIA Service的时候,是相当容易的,这里不用指定路径,也无需有其他更多设置。
得到的页面效果如下

你可能会有一个疑问,那么到底是调用了什么服务呢?
我们通过IE 9自带的开发工具,可以监控一下得到一个特殊的WCF地址

最后一行,很明显是一个WCF的调用地址,我们很好奇的是,它到底传递了什么东西过来呢?

该服务返回的其实是一个二进制的包,具体内容如下

虽然不能完全看懂,但可以确定的是,这是二进制序列化的结果。
那好,我们回到刚才的话题,既然这是一个WCF服务,而且也确实有一个隐含的svc地址(注意,我说的是隐含,因为物理上并不存在该文件),那么是否可以通过该地址,发起普通的服务调用呢?
我们尝试将http://localhost:11547/ClientBin/WebApplication1-SampleDomainService.svc 这个地址输入到浏览器地址栏

令人欣喜的是,我们看到了熟悉的WCF WSDL的界面。
那么,这是否意味着,我们可以在除了Silverlight之外的其他应用程序中调用该服务呢?
4. 创建WPF应用程序,添加服务引用
很不幸的是,我们在WPF中尝试添加服务引用时,却会遇到下面的错误

详细的错误如下,意思是说,没有找到有关的endpoint.

5. 安装RIA Service Toolkit以便添加SOAP EndPoint支持
为了解决如上的问题,微软在4月份发布了一个Toolkit,可以为RIA Service添加SOAP EndPoint支持
请先通过下面的地址下载
http://www.microsoft.com/download/en/details.aspx?id=2384
具体来说,这个工具包提供了很多增强的功能
What is available in the Toolkit?
The following functionality are available in this release of the Toolkit:-
1. LinqToSql DomainService
2. Soap endpoint - This enables you to expose a soap endpoint for your DomainService
3. JSON endpoint - This enables you to expose a JSON endpoint for your DomainService
4. ASP.NET DomainDataSource - This control will enable you to create an ASP.net application that can talk to your DomainService
5.
WCF client proxy auto generation/updating for WCF Core Service – This
enables you to get up-to-date WCF proxy and configuration each time you
build your solution, when you add Silverlight-enable WCF service in your
Silverlight application.
6. Domain Service support for Windows Azure
Table Storage. When hosting WCF RIA Services in Windows Azure, you can
now easily leverage Windows Azure Table Storage as your data backend.
(Requires Windows Azure SDK 1.3)
7. T4 template based client code
generation. The feature leverages the code generation extensibility
point added to the product and provides a well factored and granular
mechanism to customize client code generation logic using T4 templates.
8. CollectionView to enable MVVM Patterns
9. An early preview of the jQuery client for WCF RIA Services (RIA/JS)
安装好之后,我们会得到一些Assembly,在如下的目录
C:\Program Files (x86)\Microsoft SDKs\RIA Services\v1.0\Toolkit\Libraries\Server
本例中,我们将使用其中一个Microsoft.ServiceModel.DomainServices.Hosting.dll 来提供SOAP EndPoint支持,所以我们要将其添加到Web项目的引用列表中来

6. 修改Web.config,添加SOAP EndPoint
熟悉WCF的朋友都知道,WCF是基于配置的,无处不在的配置使得服务很灵活并且强大。本例中,我们也只需要修改配置文件,就可以完成添加SOAP EndPoint的工作。下面的粗体部分是我们添加的
<system.serviceModel>
<domainServices>
<endpoints>
<add name="OData" type="System.ServiceModel.DomainServices.Hosting.ODataEndpointFactory, System.ServiceModel.DomainServices.Hosting.OData, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<add name="Soap" type="Microsoft.ServiceModel.DomainServices.Hosting.SoapXmlEndpointFactory,
Microsoft.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35" />
</endpoints>
</domainServices>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
</system.serviceModel>
保存配置文件,我们再去浏览器中访问该服务。你有没有发现有什么不同呢?如果有,请告诉我吧

7. 在WPF中完成服务引用,并且使用它发起调用

是的,你没有看错,我们找到了这个服务。
好吧,既然能够添加引用,接下来的事情你应该会做了吧?当然,不是很难,你懂的
MainWindow.xaml
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid AutoGenerateColumns="True" Margin="12,62,12,12" Name="dataGrid1" />
<TextBlock Height="33" HorizontalAlignment="Left" Margin="12,23,0,0" Name="textBlock1" Text="Employee List" FontSize="25" VerticalAlignment="Top" Width="224" />
</Grid>
</Window>
MainWindow.xaml.cs
using System.Windows; namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent(); Loaded += new RoutedEventHandler(MainWindow_Loaded);
} void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
var proxy = new RIA.SampleDomainServiceSoapClient();
dataGrid1.ItemsSource = proxy.GetEmployees().RootResults;
}
}
}

BTW:我觉得WPF的界面(例如这个DataGrid)看起来没有Silverlight那么好看,不是吗? 当然,这个话题还是等我们以后有机会再来谈谈吧
总结
RIA Service默认只能被Silverlight访问,它确实极其方便。如果希望它能被其他客户代码访问到,则可以通过安装RIA Service Toolkit,为其添加SOAP Endpoint来实现。
RIA Service 的 SOAP EndPoint的更多相关文章
- 调用Ria Service中方法的各种方式
前端界面后台: using System; using System.Collections.Generic; using System.Linq; using System.Net; using S ...
- Web service standards: SOAP, REST, OData, and more
Web service standards: SOAP, REST, OData, and more So far, we've covered the components of a web ser ...
- 使用Fiddler解析WCF RIA Service传输的数据
原文 http://www.cnblogs.com/wintersun/archive/2011/01/05/1926386.html 使用Fiddler 2 解析WCF RIA Service传输的 ...
- 使用TcpTrace小工具截获Web Service的SOAP报文
Web Service客户端对服务端进行调用时,请求和响应都使用SOAP报文进行通讯.在开发和测试时,常常查看SOAP报文的内容,以便进行分析和调试.TcpTrace是一款比较小巧的工具,可以让我们截 ...
- WCF RIA SERVICE相关技术
WCF RIA SERVICE实体属性拷贝 private void DoSubmit() { ((IEditableObject)this.RepairContract).EndEdit(); va ...
- 建立自己的Web service(SOAP篇)
1.简介 这篇文章主要介绍采用SOAP来建立以及访问Web service接口. Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用 ...
- ASP.NET Web Service 标准SOAP开发案例代码(自定义验证安全头SOAPHeader)
using System.Xml;using System.Xml.Serialization;using System.Web.Services.Protocols;using System.Con ...
- Web Service之Soap请求响应内容中文编码解密
java模拟Soap请求测试Web Service接口,发现Web Service响应内容中的中文竟然是编码格式.比如: 中文:退保成功 Soap中文编码:退保成功 我仔细分析后发现,退编码实际上 ...
- 几种远程调用接口协议简单比较和web service(SOAP)与HTTP接口的区别:
什么是web service? 答:soap请求是HTTP POST的一个专用版本,遵循一种特殊的xml消息格式Content-type设置为: text/xml任何数据都可以xml化. ...
随机推荐
- struts2中#,$,%的用法以及el,ognl表达式的用法
OGNL, JSTL, STRUTS2标签中符号#,$,%的用法示例 取Session中的值 <c:out value="${sessionScope.user.userId}&quo ...
- textarea 在浏览器中固定大小和禁止拖动
HTML 标签 textarea 在大部分浏览器中只要指定行(rows)和列(cols)属性,就可以规定 textarea 的尺寸,大小就不会改变,不过更好的办法是使用 CSS 的 height 和 ...
- avalon.js实践 svg地图配置工具
MVVM模式,在很多复杂交互逻辑下面,有很大的优势.现在相关的框架也很多,现在项目中使用了avalon.js,选择它的原因,是兼容性的考虑,当然也要支持下国内开发大牛,至于性能方面的,没有实际测试过, ...
- text-indent: -999px;是什么意思
就是把该元素内的文字移到屏幕外面去,让我们肉眼看不见,有时候是因为如某栏目名称的文字或者logo的文字已经用背景图片代替了,我们不需要眼睛看见那些文字,但是希望搜索引擎可以搜到,就可以用这个把文字“隐 ...
- MVC入门
MVC开始是存在于桌面程序中的,M是指业务模型,V是指用户界面,C 则是控制器,使用MVC的目的是将M和V的实现代码分离,从而使同一个程序可以使用不同的表现形式.比如一批统计数据可以分别用柱状图.饼图 ...
- Nand ECC校验和纠错原理及2.6.27内核ECC代码分析
ECC的全称是Error Checking and Correction,是一种用于Nand的差错检测和修正算法.如果操作时序和电路稳定性不存在问题的话,NAND Flash出错的时候一般不会造成整个 ...
- COCOS2d-x简易安装步骤
准备工作:1. 下载 cocos2d-x 下载地址:http://cdn.cocos2d-x.org/cocos2d-x-2.2.zip2. 下载 python 2.7.3 下载地址:h ...
- windows设置临时环境变量path
所有在命令行下对环境变量的修改只对当前窗口有效,不是永久性的修改. 1.查看当前所有可以的环境变量:输入set查看 2.查看某个环境变量:输入 set 变量名 例如 set path 3.修改环境变量 ...
- 杭电 1272 POJ 1308 小希的迷宫
这道题是我学了并查集过后做的第三个题,教我们的学姐说这是并查集的基础题,所以有必要牢牢掌握. 下面就我做这道题的经验,给大家一些建议吧!当然,我的建议不是最好的,还请各位大神指出我的错误来,我也好改正 ...
- 强制杀oracle进程
强制杀oracle进程: for p in `ps -ef| grep ora| awk '{print $2}'`;do kill -9 $p;done 修改 oracle xe 默认中文字符集成为 ...