WCF扩展:行为扩展Behavior Extension<二>

简介:
1 引言
上回说到自定义扩展的第一步,是需要声明行为的类型。也就是通过实现一个行为接口,实现接口中的方法来声明行为的类型。
2 附件自定义行为到Operaiton或者是Endpoint
实现自定义的行为,第二步就是将自定义的行为类挂( 附加)到一个Operation或者是Endpoint上去。
自定义的行为如果是和操作有关,就附加到一个operation上;如果和具体的操作没有关系,就附加到一个endpoint上。
附加到操作需要实现System.ServiceModel.Description.IOperationBehavior接口。如果是附加到endpoint上,需要实现System.ServiceModel.Description.IEndpointBehavior接口。
如果是一个实现System.ServiceModel.Dispatcher.IClientMessageFormatter的对象,功能是序列化传输到服务端的数据。按照定义,这是一个操作相关的功能。(可以参考上一篇的定义 WCF扩展:行为扩展Behavior Extension<一>
3 通知WCF有关自定义行为的信息
实现自定义的行为,最后需要做的就是将这个行为通知WCF。让WCF知道这个行为的存在。有两种方法可以实现通知:代码和配置。
4 自定义行为示例
下面让我们来实现一个简单的自定义行为
4.1 声明
我们声明一个客户端的消息检查行为类
public  class MyClientMessageInspector:IClientMessageInspector { #region IClientMessageInspector Members  public  void AfterReceiveReply( ref System.ServiceModel.Channels.Message reply,  object correlationState) { }  public  object BeforeSendRequest( ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel) {  return  null; } #endregion }

4.2 附加
将自定义的message inspector行为附加到客户端的运行时,附加到客户端运行时client runtime的endpoint层面。
public  class MyClientMessageInspector:IClientMessageInspector,IEndpointBehavior { #region IClientMessageInspector Members  public  void AfterReceiveReply( ref System.ServiceModel.Channels.Message reply,  object correlationState) { }  public  object BeforeSendRequest( ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel) {  return  null; } #endregion #region IEndpointBehavior Members  public  void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters) { }  public  void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) { clientRuntime.MessageInspectors.Add( this); }  public  void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) { }  public  void Validate(ServiceEndpoint endpoint) { } #endregion }

4.3 通知
有两种方式来通知WCF这个自定义行为。
1 代码的方式
因为需要添加到客户端的运行时,所以需要在客户端的代码中添加。
public  interface IWcfClient { }  public  class WcfClient:ClientBase<IWcfClient >,IWcfClient {  public WcfClient( string endpointConfigurationName): base(endpointConfigurationName ) {  base.Endpoint.Behaviors.Add( new Insfrastructures.MyClientMessageInspector()); } }
就是在构造函数中添加自定义的行为。
2 通过自定义配置节的配置的方式
让我们先定义一个行为扩展配置节的类
public  class MyBehaviorExtensionElement:BehaviorExtensionElement {  public  override Type BehaviorType {  get {  return  typeof(MyClientMessageInspector); } }  protected  override  object CreateBehavior() {  return  new MyClientMessageInspector(); } }

 
 
然后再客户端的config文件中添加
<?xml version=" 1.0" encoding=" utf-8" ?> <configuration> <system.serviceModel> <client> <endpoint address=" http://localhost:8000/SimpleService/Endpoint" behaviorConfiguration=" SimpleServiceEndpointBehavior" binding=" customBinding" bindingConfiguration=" SimpleServiceBinding" contract=" Extensibility.ISimple" name=" SimpleService" /> </client> <behaviors> <endpointBehaviors> <behavior name=" SimpleServiceEndpointBehavior"> <myMessageInspector /> </behavior> </endpointBehaviors> </behaviors> <bindings> <customBinding> <binding name=" SimpleServiceBinding"> <httpTransport/> </binding> </customBinding> </bindings> <extensions> <behaviorExtensions> <add name=" myMessageInspector" type=" Insfrastructures .MyBehaviorExtensionElement, Insfrastructures .Server, Version=1.0.0.0, Culture=neutral, PublicKeyToken= null"  /> </behaviorExtensions> </extensions> </system.serviceModel> </configuration>

 



本文转自 virusswb 51CTO博客,原文链接:http://blog.51cto.com/virusswb/411490,如需转载请自行联系原作者
目录
相关文章
|
前端开发 .NET 开发框架
Wcf扩展
ASP.NET MVC和WCF真是微软两个很棒的框架,设计的很好,可扩展性非常强,到处都是横切、管道。 以前写过一篇MVC流程的文章,http://www.cnblogs.com/lovecindywang/archive/2010/12/02/1894740.html主要是使用了MVC的各种扩展。
785 0
|
.NET 网络架构
告别复杂WCF扩展 REST过程 ,让他 so easy
在看一些wcf的书和文章时,都会提到rest wcf ,实现方式多数通过对wcf进行一定程度的扩展实现的,实在是让我这种菜鸟心生畏惧~ 前些天为了体验mvc3安装了vs2010,顺便在在线模板里面搜索了一下wcf,没想到有意外发现,有图为证,这年代无图无真相   选择wcf后...
752 0
跟着Artech学习WCF扩展(1) Binding进行通信
这个demo简单 就一个服务器段的一个客户端的 主要是注释 Server的 using System; using System.Collections.Generic; using System.
689 0
跟着Artech学习WCF扩展(2) 自定义Channel与执行的顺序
源代码下载地址:点我 原文地址:http://www.cnblogs.com/artech/archive/2008/07/09/1238626.html 这节不看源码 看着真费劲哈   服务器端是这样的顺序 MyBindingElement.
691 0
跟着Artech学习WCF扩展(3) 扩展CallContextInitializer
代码下载:点我   作为备份 Artech的代码要在他的博客里面找的 Artech大师级的“总分总”式的风格,的确是和高手过招用,作为一面菜鸟,纠结了老半天才明白,原来写博客也可以前后呼应 于是把这个小节点整理到一起已被以后不时只需     5 、自定义CallContextInitializer (Step 12 & Step 18) 提到CallContextInitializer,我想有一部分人会马上想到System.Runtime.Remoting.Messaging.CallContext。
742 0
|
信息无障碍
跟着Artech学习WCF扩展(4) 扩展MessageInspector
自定义MessageInspector 在client端和service端都有自己的MessageInspector,client端叫做ClientMessageInspector,实现System.ServiceModel.Dispatcher.IClientMessageInspector interface,而service端叫做 DispatchMessageInspector, 实现了System.ServiceModel.Dispatcher.IDispatchMessageInspector interface。
845 0
|
9月前
|
前端开发
WCF更新服务引用报错的原因之一
WCF更新服务引用报错的原因之一
|
8月前
|
C# 数据安全/隐私保护
c#如何创建WCF服务到发布(SqlServer版已经验证)
c#如何创建WCF服务到发布(SqlServer版已经验证)
38 0
|
8月前
|
安全 数据库连接 数据库
WCF服务创建到发布(SqlServer版)
在本示例开始之前,让我们先来了解一下什么是wcf? wcf有哪些特点? wcf是一个面向服务编程的综合分层架构。该架构的项层为服务模型层。 使用户用最少的时间和精力建立自己的软件产品和外界通信的模型。它使得开发者能够建立一个跨平台的安全、可信赖、事务性的解决方案。且能与已有系统兼容写作。 简单概括就是:一组数据通信的应用程序开发接口。
57 0