`
tubaluer
  • 浏览: 1445940 次
文章分类
社区版块
存档分类
最新评论
  • sblig: c / c++ 是不一样的都会输出 100
    j = j++

WCF4.0 —— Routing Service

 
阅读更多

现在WCF 4.0内置了路由服务——System.ServiceModel.Routing.RoutingService,可以在 System.ServiceModel.Routing.dll 中找到。
比如下面的场景会使用到路由服务:只暴露一个外部公开的 Endpoint 映射到内部的多个的服务上。

路由服务使用的消息筛选器提供常用消息选择功能,例如,终结点的名称、SOAP 操作或消息已发送到的地址或地址前缀。也可以使用 AND 条件连接筛选器,这样,仅当消息同时与两个筛选器匹配时,才会将消息路由至某个终结点。此外,还可以通过创建自己的 MessageFilter 实现来创建自定义筛选器。(MSDN:消息筛选器)

OK,废话少说下面做一个实例来看看 RoutingService 该如何配置。
1. Console Hosting Request/Reply WCF Service Routing (示例代码下载)
(1) 创建一个普通的Wcf Service Library (使用Console Hosting)
Console Host

app.config

为了测试,Service1 使用的是 PerSession,当 Service1 被第一次调用时,服务端构造一次 Service1 实例。并在之后的调用中保持该 Session。
SetName 将保存一个值,GetName 则取出该值。

(2) 创建 Wcf Routing Service Host(使用Console Hosting)
首先添加System.ServiceModel.Routing.dll, using System.ServiceModel.Routing;

重点在于 RoutingService 的配置文件,详细参看代码中的注释说明。

(3) 创建客户端应用
首先是客户端代理的生成,通过 Wcf Routing Service 好像没有办法能访问到真实服务的wsdl,
因此我的做法是先利用真实服务的wsdl生成代理,然后修改客户端配置文件
比如这个例子中真实服务地址是:http://localhost:20000/WcfRoutingServiceTest/Service1
Wcf Routing Service的地址是: http://localhost:20002/WcfRoutingServiceTest/router/service1
修改后的客户端配置如下:
客户端调用:
(4) 运行

运行结果显示,client -> Routing Service -> Wcf Service1 注意当PerSession时,如果在RoutingService 每个filter 对于 WcfService1 都是独立的Session。
http://topic.csdn.net/u/20111005/15/69a4c0b8-63ab-4528-a520-6a8568ee17d3.html

----------------------------- 分割线 ----------------------------

2. IIS Hosting Request/Reply WCF Service Routing(示例代码下载)
当使用IIS Hosting时,我们创建的 WCF 工程模板就需要改为 Wcf Service Application了。下面介绍如何配置 Wcf Service Application 中的 Routing Service
这次需要实现的是在一个 Routing Service 中路由多个不同地址的 Wcf Service (如文章开头的图片所示)
(1) Solution Overview

其中:
WcfRoutingService :http://localhost:20001/RoutingService.svc
WcfService1 : http://localhost:20002/Service1.svc
WcfService2: http://localhost:20003/Service1.svc

(2) 配置 WcfRoutingService
因为 WcfRoutingService 里没有任何 svc 文件,这里利用了 WCF 4.0 的无 svc 文件激活服务的新特性:

另外,上面已经提到对于 WCF Request/Reply 模型的 filterTable 不允许多播,那么在 Routing Service 里,需要配置响应个数的 Endpoint
完整配置如下:
(3) 创建客户端应用
客户端配置文件
调用
(4) 运行


----------------------------- 分割线 ----------------------------

参考:

http://msdn.microsoft.com/zh-cn/library/ee517419.aspx

http://blogs.profitbase.com/tsenn/?p=23

http://msdn.microsoft.com/zh-cn/library/ee517425.aspx

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics