新版微博邀请码,[WCF 4.0新特性] 路由服务[实例篇](附9个微博邀请码)

在本篇文章中,我们将通过一个具体的实例来演示如何通过路由服务。在这个例子中,我们会创建连个简单的服务HelloServie和GoodbyeService。假设客户端不能直接调用这两个服务,需要使用到路由服务作为两者之间的中介。整个消息路由的场景如下图所示,中间的GreetingService.svc就是代表路由服务,而两个目标服务则通过HelloServie.svc和GoodbyeService.svc表示。路由服务使用的消息筛选器EndpointAddressMessageFilter,即根据包含在消息中的目标地址来决定应该将请求消息转发给HelloServie.svc还是GoodbyeService.svc。
clip_image002[WCF 4.0新特性] 路由服务[实例篇](附9个微博邀请码)新版微博邀请码

步骤一、构建解决方案

首先我们创建一个空的解决方案,并如下图所示添加三个项目并添加相应的引用。其中类库项目Service.Interface和Service分别用于定义服务契约和服务类型,而控制台项目Client用作为进行服务调用的客户端。
clip_image003clip_image002[WCF 4.0新特性] 路由服务[实例篇](附9个微博邀请码)新版微博邀请码

步骤二、定义服务

在Service.Interface项目中为两个服务创建服务契约接口:IHello和IGoodbye,两个接口定义如下。
1: using System.ServiceModel;
2: namespace Artech.RoutingServiceDemo.Service.Interface
3: {
4: [ServiceContract(Namespace="http://www.artech.com/")]
5: public interface IHello
6: {
7: [OperationContract]
8: string SayHello(string userName);
9: }
10: [ServiceContract(Namespace = "http://www.artech.com/")]
11: public interface IGoodbye
12: {
13: [OperationContract]
14: string SayGoodbye(string userName);
15: }
16: }
然后在Service项目中定义实现两个服务契约接口的服务类型:HelloService和GoodbyeService,具体定义如下。
1: using Artech.RoutingServiceDemo.Service.Interface;
2: namespace Service
3: {
4: public class HelloService: IHello
5: {
6: public string SayHello(string userName)
7: {
8: return string.Format("Hello, {0}", userName);
9: }
10: }
11: public class GoodbyeService : IGoodbye
12: {
13: public string SayGoodbye(string userName)
14: {
15: return string.Format("Goodbye, {0}", userName);
16: }
17: }
18: }

步骤三、寄宿目标服务和路由服务

我们将上面定义的两个服务HelloService和GoodbyeService,以及路由服务RoutingService寄宿在IIS下。为此,我们直接在IIS管理器中创建一个Web应用(起名为“RoutingServiceDemo”),其物理地址之上Service项目的根目录。然后,不要忘了将该项目的编译后的输出目录从默认的\bin\Debug\改为\bin。接下来在Service项目中添加一个Web.config, 并完成如下的配置。
1:
2:
3:
4:
5:
6:
7:

8:

9:

10:
11:
12:
13:

14:
15:
16:

17:
18:
19:

20:

21:
22:
23:
24:

25:
26:
27:
28:
29:

30:
31:
32:
33:
34:

35:

36:

37:
38:
39:
40:
41:
43:

44:

45:

46:

我们对上述的这段配置进行一下简单的分析。首先,我们按照“无.svc文件服务激活”方式(《标准终结点与无(.SVC)文件服务激活》)对服务HelloService、GoodbyeService和路由服务RoutingService进行寄宿,它们的相对地址分别为HelloService.svc、GoodbyeService.svc和GreetingService.svc。它们都具有一个唯一的基于WS2007HttpBinding的终结点。由于我们需要路由服务采用请求/回复模式进行消息路由,我们将契约指定为IRequestReplyRouter。
1:
2:
3:
4:
5:
6:

7:
8:
9:

10:
11:
12:

13:

14:
15:
16:
17:
18:
20:

21:

22: ...
23:

24:

路由服务上应用了服务行为RoutingBehavior,配置在该行为上名称为greetingFilterTable的筛选器表定义如下。该筛选器表它具有两个基于EndpointAddressMessageFilter的消息筛选器,配置名称分别为“Address4HelloService”和“Address4GoodbyeService”,它们分别将目标服务HelloService和GoodbyeService的地址作为筛选条件。而这两个筛选器对应着指向目标服务的客户端终结点“helloService”和“goodbyeService”。
1:
2:
3:
4:
5:
6:

7:
8:
9:
10:
11:

12:
13:
14:
15:
16:

17:

18:

19:

20:

对于上述的配置,细心的读者也许发现一个特殊之处:定义在配置节下的被路由服务使用的终结点的契约被设置成“*”(contract=”*”)。照理说,这里的契约应该设置成路由服务实现的服务契约System.ServiceModel.Routing.IRequestReplyRouter才对。但是你真的进行了如此的设置,因为将路由服务使用的客户端终结点契约设置成“*”是个强制性的规定。

步骤四、服务调用

由于调用服务的消息需要通过路由服务这个中介才能抵达真正的目标服务,所以客户端我们需要将路由服务的地址作为消息发送的目标地址。在这里,我们通过ClientViaBehavior这个终结点行为实现了物理地址(消息真正发送的目标地址)和逻辑地址(终结点地址)的分离,将消息发送给路由服务的地址:http://127.0.0.1/RoutingServiceDemo/GrettingService.svc。下面的XML片断代表整个客户端的配置,而ClientViaBehavior被定义成默认的终结点行为。
1:
2:
3:
4:
5:
6:
7:

8:

9:

10:
11: 12: address="http://127.0.0.1/RoutingServiceDemo/HelloService.svc"
13: binding="ws2007HttpBinding"
14: contract="Artech.RoutingServiceDemo.Service.Interface.IHello"/>
15: 16: address="http://127.0.0.1/RoutingServiceDemo/GoodbyeService.svc"
17: binding="ws2007HttpBinding"
18: contract="Artech.RoutingServiceDemo.Service.Interface.IGoodbye"/>
19:

20:

21:

借助于这样的配置,你可以按照传统的编程方式进行服务的调用,无需再考虑底层消息路由机制的存在。
1: using (ChannelFactory channelFactoryHello = new ChannelFactory("helloService"))
2: using (ChannelFactory channelFactoryGoodbye = new ChannelFactory("goodbyeService"))
3: {
4: IHello helloProxy = channelFactoryHello.CreateChannel();
5: IGoodbye goodbyeProxy = channelFactoryGoodbye.CreateChannel();
6: Console.WriteLine(helloProxy.SayHello("Zhang San"));
7: Console.WriteLine(goodbyeProxy.SayGoodbye("Li Si"));
8: }
输出结果:
1: Hello, Zhang San
2: Goodbye, Li Si
新版微博邀请码: http://weibo.com/upcode/dgLO4OfiD http://weibo.com/upcode/dgLOZvueN http://weibo.com/upcode/dgLOqNjas http://weibo.com/upcode/dgLOlZrRd http://weibo.com/upcode/dgLOhgYsJ http://weibo.com/upcode/dgLOdtS4I http://weibo.com/upcode/dgLO5sRee http://weibo.com/upcode/dgLO7vwqF http://weibo.com/upcode/dgLO7zMyr
Tags:  微博邀请码怎么用 微博邀请码是什么 微博邀请码 新浪微博邀请码 新版微博邀请码

延伸阅读

最新评论

发表评论