使用消息分发检查器、服务器行为、端点行为扩展WCF的消息分发行为
Extend WCF dispatch message behavior with IDispatchMessageInspector IServiceBehavior, IEndpointBehavior
WCF从绑定的通道中接受到客户端调用它的消息后,使用消息分发器将消息发给需要调用的操作。这个过程中我们在不修改原有WCF的程序也不需要加入标签,只需要在WCF的配置文件中加入需要扩展的配置可以实现扩展WCF消息分发。
在我的上一篇博克中,我们可以拦截获取操作参数,在操作调用前和调用后加入自己的扩展,但是这种方法需要在WCF契约的操作中加入标签,这种方法我认为是一种“静态植入”,这种方法需要重新编译。
使用IParameterInspector, IOperationBehavior,Attribute(参数检查器、操作行为接口和标签)扩展WCF操作行为
地址:http://www.cnblogs.com/utopia/archive/2009/12/02/1615280.html
本文讲述使用消息扩展,不需要重新编译,修改配置文件就可以,应该是“动态拦截”的方法。
实现扩展消息分发需要4步:1、实现IDispatchMessageInspector;2、实现Attribute, IServiceBehavior, IEndpointBehavior;3、BehaviorExtensionElement中加入实现的ServiceBehavior和EndPointBehavior;4、在配置文件中加入扩展行为
1、实现IDispatchMessageInspector;
2 {
3 public object AfterReceiveRequest(
4 ref System.ServiceModel.Channels.Message request, IClientChannel channel, InstanceContext instanceContext)
5 {
6 MessageQueue mq = new MessageQueue(@".\private$\msgqueue");
7 mq.Send("RelSrv get msg from "+"channel.RemoteAddress.ToString()");
8 return null;
9 }
10
11 public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
12 {
13 MessageQueue mq = new MessageQueue(@".\private$\msgqueue");
14 mq.Send("RelSrv sent msg to " + "reply");
15 }
16 }
2、实现Attribute, IServiceBehavior, IEndpointBehavior;
2 {
3 void IServiceBehavior.AddBindingParameters(ServiceDescription serviceDescription,
4 ServiceHostBase serviceHostBase,
5 System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints,
6 BindingParameterCollection bindingParameters)
7 {
8 }
9 void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription serviceDescription,
10 ServiceHostBase serviceHostBase)
11 {
12 WsdlExporter wsdlExporter = new WsdlExporter();
13 wsdlExporter.ExportEndpoints(serviceDescription.Endpoints,
14 new XmlQualifiedName(serviceDescription.Name, serviceDescription.Namespace));
15
16 foreach (ChannelDispatcher cDispatcher in serviceHostBase.ChannelDispatchers)
17 foreach (EndpointDispatcher endpointDispatcher in cDispatcher.Endpoints)
18 endpointDispatcher.DispatchRuntime.MessageInspectors.Add(
19 new RelMsgInspector( ));
20 }
21 void IServiceBehavior.Validate(ServiceDescription serviceDescription,
22 ServiceHostBase serviceHostBase)
23 {
24 }
25 void IEndpointBehavior.AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
26 {
27 }
28 void IEndpointBehavior.ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
29 {
30 }
31 void IEndpointBehavior.ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
32 {
33 WsdlExporter wsdlExporter = new WsdlExporter();
34 wsdlExporter.ExportEndpoint(endpoint);
35 endpointDispatcher.DispatchRuntime.MessageInspectors.Add(
36 new RelMsgInspector());
37 }
38 void IEndpointBehavior.Validate(ServiceEndpoint endpoint)
39 {
40 }
41 }
3、BehaviorExtensionElement中加入实现的ServiceBehavior和EndPointBehavior;
2 {
3 public override Type BehaviorType
4 {
5 get { return typeof(RelMsgValidation); }
6 }
7 protected override object CreateBehavior()
8 {
9 return new RelMsgValidation();
10 }
11 }
4、在配置文件中加入扩展行为
在配置中定义扩展
<behaviorExtensions>
<add name="RelMsgBehavior" type="RelSrvExtends.RelMsgBehaviorExtension, RelSrvExtends, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</behaviorExtensions>
</extensions>
在Endpointbehavior 或 Servicebehavior中加入扩展
代码
<endpointBehaviors>
<behavior name="RelSrvMsgBehavior">
<RelMsgBehavior />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="DefaultBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="false" />
<serviceDebug httpsHelpPageEnabled="true" includeExceptionDetailInFaults="true" />
<RelMsgBehavior />
</behavior>
</serviceBehaviors>
</behaviors>
在Service和Endpoint的配置节中加入上面的behavior
<endpoint address="" behaviorConfiguration="RelSrvMsgBehavior"
binding="basicHttpBinding" name="basicHttpBinding" contract="RelSrvContract.IRelSrvContract" />
当然我们也可以使用VS2008 自带的 WCF 配置文件管理器 SvcConfigEditor.exe 来添加扩展:
配置中定义扩展
在Endpointbehavior 或 Servicebehavior中加入扩展
在Service和Endpoint的配置节中加入上面的behavior
to be Continued