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
{
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文件中添加
<!--l version="<span style="color: #8b0000" mce_style="color: #8b000-->1.0" encoding="utf-8" ?>
http://localhost:8000/SimpleService/Endpoint"
behaviorConfiguration="SimpleServiceEndpointBehavior"
binding="customBinding"
bindingConfiguration="SimpleServiceBinding"
contract="Extensibility.ISimple"
name="SimpleService" />
SimpleServiceEndpointBehavior">
SimpleServiceBinding">
myMessageInspector"
type="Insfrastructures
.MyBehaviorExtensionElement, Insfrastructures
.Server, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
在 配置节添加行为扩展的配置节。然后在endpoint的behavior配置节中指定使用扩展中的行为。
分享到:
相关推荐
一个非常简单的WCF例子:Hello World 本解决方案有3个项目: 1、WCFClient:客户端,展示通过wcf后得到的结果,一个小的winform; 2、WCFContrlPanel:主控程序,作为打开/关闭wcf服务的控制面板,winform; 3、...
Professional WCF 4: Windows Communication Foundation with .NET 4 (Wrox Programmer to Programmer)
WCF编程:WCF服务和客户端的建立[归纳].pdf
该程序创建一个完整的WCF应用,应用功能虽然简单,但它涵盖了一个完整WCF应用的基本结构。对那些对WCF不是很了解的读者来说,这个例子将带领你正式进入WCF的世界。
wcf扩展性的例子
这是在学习WebCast上的【构建WCF面向服务的应用程序】时写的DEMO,对应第一节课【WCF概述】
WCF is the technology in .NET that you use to build service - oriented applications, to exchange messages in various communication scenarios, and to run workfl ows built from service operations. With...
WCF4高级编程Professional.WCF.4.rar
讲解WCF技术的,适合各种阶段的学习。从入门到进阶。
jquery 调用WCF :多参数 WCF服务接口,创建服务,jquery 通过post方式取得数据
(15):扩展性-WCF课件-《构建WCF面向服务的应用程序》
Programming WCF Services is the authoritative, bestselling guide to Microsoft's unified platform for developing modern service-oriented applications on Windows. Hailed as the definitive treatment of ...
WCF:Windows平台新一代通讯基础研究与分析
GRIN-Global-WCFService:用于GRIN-Global平台的REST Web服务用途:扩展GRIN-Global中间层和移动应用程序之间的互操作性。 开发工具:Visual Studio 2019社区和.NET 4.6.2
WCF:Windows平台新一代通讯基础研究与分析
WcfServiceLibrary1--- wcf服务:生成dll文件给宿主引用 WinFormsWcfService--- 宿主 WcfClient---调用wcf服务客户端
此扩展增加了对WCF服务和行为的依赖项注入的支持。 有3个Ninject ServiceHostFactory可以在和。 Ninject.Extensions.Wcf.NinjectServiceHostFactory用于普通服务 Ninject.Extensions.Wcf....
为了使读者对基于WCF的编程模型有一个直观的映像,我将带领读者一步一步地创建一个完整的WCF应用。本应用功能虽然简单,但它涵盖了一个完整WCF应用的基本结构。对那些对WCF不是很了解的读者来说,这个例子将带领你...
WCF之旅:一个简单的WCF程序(vs2010源码) 文章 + 源码 入门首选文章,折腾了好久才折腾出第一个wcf程序。 对准备学习wcf的人员绝对有意义
1:创建第一个WCF服务 2:使用IIS发布WCF服务 3:自运行WCF服务 4:使用Windows服务发布WCF服务 5:创建WCF客户端程序