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>
在<extensions> 配置节添加行为扩展的配置节。然后在endpoint的behavior配置节中指定使用扩展中的行为。
分享到:
相关推荐
本期MSDN杂志的主要内容有:<br><br>• 自己动手: 创建 .NET Framework 语言编译器<br><br>• WinUnit: 简化的本机 C++ 应用程序单元测试<br><br>• Silverlight: 创建自定义 Expression Encoder 发布插件<br><br>•...
本期的主要内容:<br><br>移动 WCF: 使用 .NET Compact Framework 编写 IM 应用程序<br>.NET Compact Framework 3.5 是 Windows Communication Foundation (WCF) 的功能子集,可以用来控制 Windows Mobile 设备与...
一个非常简单的WCF例子:Hello World 本解决方案有3个项目: 1、WCFClient:客户端,展示通过wcf后得到的结果,一个小的winform; 2、WCFContrlPanel:主控程序,作为打开/关闭wcf服务的控制面板,winform; 3、...
AutomaticFormatSelection WCF服务源程序及客户端源程序 <?xml version="1.0" encoding="utf-8" ?> <configuration> ... <service name="Artech.WcfServices.Service.EmployeesService"> <endpoint address="htt
Professional WCF 4: Windows Communication Foundation with .NET 4 (Wrox Programmer to Programmer)
WCF编程:WCF服务和客户端的建立[归纳].pdf
wcf扩展性的例子
(15):扩展性-WCF课件-《构建WCF面向服务的应用程序》<br>
该程序创建一个完整的WCF应用,应用功能虽然简单,但它涵盖了一个完整WCF应用的基本结构。对那些对WCF不是很了解的读者来说,这个例子将带领你正式进入WCF的世界。
这是在学习WebCast上的【构建WCF面向服务的应用程序】时写的DEMO,对应第一节课【WCF概述】
经典视频教程,希望大家好好利用<br><br>讲 师:徐晓卓 微软最有价值专家 长沙萌微科技有限公司首席技术顾问 <br> <br>课程简介:本次系列课程将采用案例教学的方法,深度剖析微软的基于.NET 3.0的分布式应用设计...
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 & WPF 聊天程序源码<br>For those that have read some of my other CodeProject articles you will probably know, that I am not shy about trying out new technologies. But one good thing about that is ...
此扩展增加了对WCF服务和行为的依赖项注入的支持。 有3个Ninject ServiceHostFactory可以在和。 Ninject.Extensions.Wcf.NinjectServiceHostFactory用于普通服务 Ninject.Extensions.Wcf....
讲解WCF技术的,适合各种阶段的学习。从入门到进阶。
jquery 调用WCF :多参数 WCF服务接口,创建服务,jquery 通过post方式取得数据
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 ...
SoapClient - WinRT SOAP 客户端 Windows Phone 8.1 不包含 ... <metadataDocumentPath> - 元数据文档 (wsdl) 的路径<file> - 输出文件路径<namespace> - 输出文件命名空间<svcutil> - SvcUtil.exe 路径 示例:Soa