WCF 第五章 行为 以属性为服务操作行为暴露一个参数检测器
2011-06-30 10:00
489 查看
列表5.24实现一个使用正则表达式验证参数的行为。它可以应用到任何操作上并允许开发人员定义一个正则表达式和当参数不合法时用来返回错误信息的消息。
代码显示了从一个操作行为调用的一个参数检测器并显示了实现一个属性的操作行为。它也显示了如何通过在服务定义中引用属性来讲操作行为添加到服务描述中。
类myParameterInspector实现了IParemeterInspector接口。类存储了两个本地属性,_pattern和 _message,用来在BeforeCall方法中验证参数。在那个方法中,使用正则表达式来匹配参数值和参数pattern.如果值与pattern 不符合,会抛出一个错误。
类myOperationBehavior是吸纳了IEndpointBehavior和Attribute接口。在 AddDispatchBehavior方法中它添加myParameterInspector类到将要被每个操作调用的参数检测器列表中去。最后,当服 务操作时,GetPrice,被定义,myOperationBehavior属性用来在运行时验证它的参数。
列表5.24 在一个操作行为中以属性暴露的自定义参数检测器
view sourceprint?
======
转载自
作者:DanielWise
出处:http://www.cnblogs.com/danielWise/
代码显示了从一个操作行为调用的一个参数检测器并显示了实现一个属性的操作行为。它也显示了如何通过在服务定义中引用属性来讲操作行为添加到服务描述中。
类myParameterInspector实现了IParemeterInspector接口。类存储了两个本地属性,_pattern和 _message,用来在BeforeCall方法中验证参数。在那个方法中,使用正则表达式来匹配参数值和参数pattern.如果值与pattern 不符合,会抛出一个错误。
类myOperationBehavior是吸纳了IEndpointBehavior和Attribute接口。在 AddDispatchBehavior方法中它添加myParameterInspector类到将要被每个操作调用的参数检测器列表中去。最后,当服 务操作时,GetPrice,被定义,myOperationBehavior属性用来在运行时验证它的参数。
列表5.24 在一个操作行为中以属性暴露的自定义参数检测器
01 | [AttributeUsage(AttributeTargets.Method)] |
02 | public class myOperationBehavior : Attribute, IOperationBehavior |
03 | { |
04 | public string pattern; |
05 | public string message; |
06 |
07 | public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters) |
08 | { |
09 | } |
10 | public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation) |
11 | { |
12 | } |
13 | public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation) |
14 | { |
15 | dispatchOperation.ParameterInspectors.Add( new myParameterInspector( this .pattern, this .message)); |
16 | } |
17 | public void Validate(OperationDescription operationDescription) |
18 | { |
19 | } |
20 | } |
01 | class myParameterInspector : IParameterInspector |
02 | { |
03 | string _pattern; |
04 | string _message; |
05 | public myParameterInspector( string pattern, string message) |
06 | { |
07 | _pattern = pattern; |
08 | _message = message; |
09 | } |
10 | public void AfterCall( string operationName, object [] outputs, object returnValue, object correlationState) |
11 | { |
12 | } |
13 |
14 | public object BeforeCall( string operationName, object [] inputs) |
15 | { |
16 | foreach ( object input in inputs) |
17 | { |
18 | if ((input!= null ) && (input.GetType() == typeof ( string ))) |
19 | { |
20 | Regex regex = new Regex(_pattern); |
21 | if (regex.IsMatch(( string )input)) |
22 | { |
23 | throw new FaultException( string .Format( "Parameter out of range:{0}, {1}" , ( string )input, _message)); |
24 | } |
25 | } |
26 | } |
27 | return null ; |
28 | } |
29 | } |
01 | [ServiceContract] |
02 | public interface IStockService |
03 | { |
04 | [OperationContract] |
05 | double GetPrice( string ticker); |
06 | } |
07 |
08 | public class StockService : IStockService |
09 | { |
10 | [myOperationBehavior(pattern = "[^a-zA-Z]" , message = "Only alpha characters allowed" )] |
11 | public double GetPrice( string ticker) |
12 | { |
13 | if (ticker == "MSFT" ) |
14 | { |
15 | return 94.85; |
16 | } |
17 | else |
18 | { |
19 | return 0.0; |
20 | } |
21 | } |
22 | } |
转载自
作者:DanielWise
出处:http://www.cnblogs.com/danielWise/
相关文章推荐
- WCF 第五章 行为 以属性为服务操作行为暴露一个参数检测器
- WCF 第五章 行为 通过配置文件暴露一个服务行为
- WCF 第五章 行为 通过配置文件暴露一个服务行为
- WCF 第五章 行为 为服务终结点行为实现一个消息检测器
- WCF 第五章 行为 在WCF一个服务内部的事务操作
- WCF 第五章 行为 在WCF一个服务内部的事务操作
- WCF 第五章 行为 为服务终结点行为实现一个消息检测器
- WCF热门问题编程示例(2)多个实例调用一个WCF服务操作,需要等待服务响应吗
- WCF学习心得(1): 获取一个服务所有的契约中的操作
- WCF热门问题编程示例(2)多个实例调用一个WCF服务操作,需要等待服务响应吗
- WCF 第五章 行为 实现事务(操作行为)
- WCF 第五章 导出并发布元数据(服务行为)
- WCF 第五章 行为 事务之选择一个事务协议-OleTx 或者WS-AT
- WCF 第五章 行为 事务-跨操作事务流
- WCF 第四章 绑定 在多个绑定上暴露一个服务契约
- WCF热门问题编程示例(2)多个实例调用一个WCF服务操作,需要等待服务响应吗
- WCF 第五章 行为 事务之事务服务行为
- WCF热门问题编程示例(2)多个实例调用一个WCF服务操作,需要等待服务响应吗
- WCF 第五章 行为 实现事务(操作行为)
- WCF 第十一章 工作流服务 从WF暴露一个服务