[WCF 4.0新特性] 路由服务[实例篇]
2011-10-28 14:20
495 查看
在本篇文章中,我们将通过一个具体的实例来演示如何通过路由服务。在这个例子中,我们会创建连个简单的服务HelloServie和GoodbyeService。假设客户端不能直接调用这两个服务,需要使用到路由服务作为两者之间的中介。整个消息路由的场景如下图所示,中间的GreetingService.svc就是代表路由服务,而两个目标服务则通过HelloServie.svc和GoodbyeService.svc表示。路由服务使用的消息筛选器EndpointAddressMessageFilter,即根据包含在消息中的目标地址来决定应该将请求消息转发给HelloServie.svc还是GoodbyeService.svc。[源代码从这里下载]
[/code]
然后在Service项目中定义实现两个服务契约接口的服务类型:HelloService和GoodbyeService,具体定义如下。
[/code]
[/code]
我们对上述的这段配置进行一下简单的分析。首先,我们按照“无.svc文件服务激活”方式(《标准终结点与无(.SVC)文件服务激活》)对服务HelloService、GoodbyeService和路由服务RoutingService进行寄宿,它们的相对地址分别为HelloService.svc、GoodbyeService.svc和GreetingService.svc。它们都具有一个唯一的基于WS2007HttpBinding的终结点。由于我们需要路由服务采用请求/回复模式进行消息路由,我们将契约指定为IRequestReplyRouter。
[/code]
路由服务上应用了服务行为RoutingBehavior,配置在该行为上名称为greetingFilterTable的筛选器表定义如下。该筛选器表它具有两个基于EndpointAddressMessageFilter的消息筛选器,配置名称分别为“Address4HelloService”和“Address4GoodbyeService”,它们分别将目标服务HelloService和GoodbyeService的地址作为筛选条件。而这两个筛选器对应着指向目标服务的客户端终结点“helloService”和“goodbyeService”。
[/code]
对于上述的配置,细心的读者也许发现一个特殊之处:定义在<client>配置节下的被路由服务使用的终结点的契约被设置成“*”(contract=”*”)。照理说,这里的契约应该设置成路由服务实现的服务契约System.ServiceModel.Routing.IRequestReplyRouter才对。但是你真的进行了如此的设置,因为将路由服务使用的客户端终结点契约设置成“*”是个强制性的规定。
[/code]
借助于这样的配置,你可以按照传统的编程方式进行服务的调用,无需再考虑底层消息路由机制的存在。
[/code]
输出结果:
[/code]
步骤一、构建解决方案
首先我们创建一个空的解决方案,并如下图所示添加三个项目并添加相应的引用。其中类库项目Service.Interface和Service分别用于定义服务契约和服务类型,而控制台项目Client用作为进行服务调用的客户端。步骤二、定义服务
在Service.Interface项目中为两个服务创建服务契约接口:IHello和IGoodbye,两个接口定义如下。[code] using System.ServiceModel;
namespace Artech.RoutingServiceDemo.Service.Interface
{
[ServiceContract(Namespace="http://www.artech.com/")]
public interface IHello
{
[OperationContract]
string SayHello(string userName);
}
[ServiceContract(Namespace = "http://www.artech.com/")]
public interface IGoodbye
{
[OperationContract]
string SayGoodbye(string userName);
}
}
[/code]
然后在Service项目中定义实现两个服务契约接口的服务类型:HelloService和GoodbyeService,具体定义如下。
[code] using Artech.RoutingServiceDemo.Service.Interface;
namespace Service
{
public class HelloService: IHello
{
public string SayHello(string userName)
{
return string.Format("Hello,{0}", userName);
}
}
public class GoodbyeService : IGoodbye
{
public string SayGoodbye(string userName)
{
return string.Format("Goodbye,{0}", userName);
}
}
}
[/code]
步骤三、寄宿目标服务和路由服务
我们将上面定义的两个服务HelloService和GoodbyeService,以及路由服务RoutingService寄宿在IIS下。为此,我们直接在IIS管理器中创建一个Web应用(起名为“RoutingServiceDemo”),其物理地址之上Service项目的根目录。然后,不要忘了将该项目的编译后的输出目录从默认的\bin\Debug\改为\bin。接下来在Service项目中添加一个Web.config, 并完成如下的配置。[code] <configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="routingBehavior">
<routing filterTableName="greetingFilterTable" routeOnHeadersOnly="true" soapProcessingEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="Service.HelloService">
<endpoint binding="ws2007HttpBinding" contract="Artech.RoutingServiceDemo.Service.Interface.IHello" />
</service>
<service name="Service.GoodbyeService">
<endpoint binding="ws2007HttpBinding" contract="Artech.RoutingServiceDemo.Service.Interface.IGoodbye" />
</service>
<service behaviorConfiguration="routingBehavior" name="System.ServiceModel.Routing.RoutingService">
<endpoint binding="ws2007HttpBinding" contract="System.ServiceModel.Routing.IRequestReplyRouter" />
</service>
</services>
<client>
<endpoint name="helloService" address="http://127.0.0.1/RoutingServiceDemo/HelloService.svc" binding="ws2007HttpBinding" contract="*"/>
<endpoint name="goodbyeService" address="http://127.0.0.1/RoutingServiceDemo/GoodbyeService.svc" binding="ws2007HttpBinding" contract="*"/>
</client>
<routing>
<filters>
<filter name ="Address4HelloService" filterType ="EndpointAddress" filterData="http://127.0.0.1/RoutingServiceDemo/HelloService.svc"/>
<filter name ="Address4GoodbyeService" filterType ="EndpointAddress" filterData="http://127.0.0.1/RoutingServiceDemo/GoodbyeService.svc"/>
</filters>
<filterTables>
<filterTable name="greetingFilterTable">
<add filterName="Address4HelloService" endpointName="helloService"/>
<add filterName="Address4GoodbyeService" endpointName="goodbyeService"/>
</filterTable>
</filterTables>
</routing>
<serviceHostingEnvironment>
<serviceActivations>
<add relativeAddress="HelloService.svc" service="Service.HelloService"/>
<add relativeAddress="GoodbyeService.svc" service="Service.GoodbyeService"/>
<add relativeAddress="GrettingService.svc" service="System.ServiceModel.Routing.RoutingService,
System.ServiceModel.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</serviceActivations>
</serviceHostingEnvironment>
</system.serviceModel>
</configuration>
[/code]
我们对上述的这段配置进行一下简单的分析。首先,我们按照“无.svc文件服务激活”方式(《标准终结点与无(.SVC)文件服务激活》)对服务HelloService、GoodbyeService和路由服务RoutingService进行寄宿,它们的相对地址分别为HelloService.svc、GoodbyeService.svc和GreetingService.svc。它们都具有一个唯一的基于WS2007HttpBinding的终结点。由于我们需要路由服务采用请求/回复模式进行消息路由,我们将契约指定为IRequestReplyRouter。
[code] <configuration>
<system.serviceModel>
<services>
<service name="Service.HelloService">
<endpoint binding="ws2007HttpBinding" contract="Artech.RoutingServiceDemo.Service.Interface.IHello" />
</service>
<service name="Service.GoodbyeService">
<endpoint binding="ws2007HttpBinding" contract="Artech.RoutingServiceDemo.Service.Interface.IGoodbye" />
</service>
<service behaviorConfiguration="routingBehavior" name="System.ServiceModel.Routing.RoutingService">
<endpoint binding="ws2007HttpBinding" contract="System.ServiceModel.Routing.IRequestReplyRouter" />
</service>
</services>
<serviceHostingEnvironment>
<serviceActivations>
<add relativeAddress="HelloService.svc" service="Service.HelloService"/>
<add relativeAddress="GoodbyeService.svc" service="Service.GoodbyeService"/>
<add relativeAddress="GrettingService.svc" service="System.ServiceModel.Routing.RoutingService,
System.ServiceModel.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</serviceActivations>
</serviceHostingEnvironment>
...
</system.serviceModel>
</configuration>
[/code]
路由服务上应用了服务行为RoutingBehavior,配置在该行为上名称为greetingFilterTable的筛选器表定义如下。该筛选器表它具有两个基于EndpointAddressMessageFilter的消息筛选器,配置名称分别为“Address4HelloService”和“Address4GoodbyeService”,它们分别将目标服务HelloService和GoodbyeService的地址作为筛选条件。而这两个筛选器对应着指向目标服务的客户端终结点“helloService”和“goodbyeService”。
[code] <configuration>
<system.serviceModel>
<client>
<endpoint name="helloService" address="http://127.0.0.1/RoutingServiceDemo/HelloService.svc" binding="ws2007HttpBinding" contract="*"/>
<endpoint name="goodbyeService" address="http://127.0.0.1/RoutingServiceDemo/GoodbyeService.svc" binding="ws2007HttpBinding" contract="*"/>
</client>
<routing>
<filters>
<filter name ="Address4HelloService" filterType ="EndpointAddress" filterData="http://127.0.0.1/RoutingServiceDemo/HelloService.svc"/>
<filter name ="Address4GoodbyeService" filterType ="EndpointAddress" filterData="http://127.0.0.1/RoutingServiceDemo/GoodbyeService.svc"/>
</filters>
<filterTables>
<filterTable name="greetingFilterTable">
<add filterName="Address4HelloService" endpointName="helloService"/>
<add filterName="Address4GoodbyeService" endpointName="goodbyeService"/>
</filterTable>
</filterTables>
</routing>
</system.serviceModel>
</configuration>
[/code]
对于上述的配置,细心的读者也许发现一个特殊之处:定义在<client>配置节下的被路由服务使用的终结点的契约被设置成“*”(contract=”*”)。照理说,这里的契约应该设置成路由服务实现的服务契约System.ServiceModel.Routing.IRequestReplyRouter才对。但是你真的进行了如此的设置,因为将路由服务使用的客户端终结点契约设置成“*”是个强制性的规定。
步骤四、服务调用
由于调用服务的消息需要通过路由服务这个中介才能抵达真正的目标服务,所以客户端我们需要将路由服务的地址作为消息发送的目标地址。在这里,我们通过ClientViaBehavior这个终结点行为实现了物理地址(消息真正发送的目标地址)和逻辑地址(终结点地址)的分离,将消息发送给路由服务的地址:http://127.0.0.1/RoutingServiceDemo/GrettingService.svc。下面的XML片断代表整个客户端的配置,而ClientViaBehavior被定义成默认的终结点行为。[code] <configuration>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior>
<clientVia viaUri="http://127.0.0.1/RoutingServiceDemo/GrettingService.svc"/>
</behavior>
</endpointBehaviors>
</behaviors>
<client>
<endpoint name="helloService"
address="http://127.0.0.1/RoutingServiceDemo/HelloService.svc"
binding="ws2007HttpBinding"
contract="Artech.RoutingServiceDemo.Service.Interface.IHello"/>
<endpoint name="goodbyeService"
address="http://127.0.0.1/RoutingServiceDemo/GoodbyeService.svc"
binding="ws2007HttpBinding"
contract="Artech.RoutingServiceDemo.Service.Interface.IGoodbye"/>
</client>
</system.serviceModel>
</configuration>
[/code]
借助于这样的配置,你可以按照传统的编程方式进行服务的调用,无需再考虑底层消息路由机制的存在。
[code] using (ChannelFactory<IHello> channelFactoryHello = new ChannelFactory<IHello>("helloService"))
using (ChannelFactory<IGoodbye> channelFactoryGoodbye = new ChannelFactory<IGoodbye>("goodbyeService"))
{
IHello helloProxy = channelFactoryHello.CreateChannel();
IGoodbye goodbyeProxy = channelFactoryGoodbye.CreateChannel();
Console.WriteLine(helloProxy.SayHello("Zhang San"));
Console.WriteLine(goodbyeProxy.SayGoodbye("Li Si"));
}
[/code]
输出结果:
[code] Hello, Zhang San
Goodbye, Li Si
[/code]
相关文章推荐
- [WCF 4.0新特性] 路由服务[实例篇]
- [WCF 4.0新特性] 路由服务[原理篇]
- WCF 4.0 进阶系列 – 第十四章 检测服务和路由消息(第三部分)
- [WCF 4.0新特性] 标准终结点与无(.SVC)文件服务激活
- WCF 4.0 进阶系列 – 第十四章 检测服务和路由消息(第一部分)
- WCF 4.0 进阶系列 – 第十四章 检测服务和路由消息(第四部分)
- WCF 4.0 进阶系列 – 第十四章 检测服务和路由消息(第二部分)
- WCF4.0新特性体验(5):路由服务Routing Service(上)
- “WCF 4.0新特性”系列[共12篇]
- WCF 4.0新特性汇总[共12篇]
- WCF4.0新特性体验(5):路由服务Routing Service(上)
- WCF4.0新特性体验(6):路由服务Routing Service(下)
- 精进不休 .NET 4.0 (2) - asp.net 4.0 新特性之url路由, 自定义CacheProvider, 新增的表达式<%: expression %>, QueryExtender控
- 精进不休 .NET 4.0 (2) - asp.net 4.0 新特性之url路由, 自定义CacheProvider, 新增的表达式<%: expression %>, QueryExtender控
- WCF - WCF 4.0 新特性
- [WCF 4.0新特性] 默认绑定和行为配置
- IIS7部署.NET 4.0 WCF服务提示"svc integrated" - "bad module"错误
- WCF 4.0 进阶系列 – 第十五章 构建REST服务(第二部分)
- WCF4.0 –- RESTful WCF Services (实例) (并发同步服务 SyncService)
- WCF4.0新特性体验(6):路由服务Routing Service(下)