WCF 第五章 行为 以属性为服务操作行为暴露一个参数检测器

列表5.24实现一个使用正则表达式验证参数的行为。它可以应用到任何操作上并允许开发人员定义一个正则表达式和当参数不合法时用来返回错误信息的消息。

  代码显示了从一个操作行为调用的一个参数检测器并显示了实现一个属性的操作行为。它也显示了如何通过在服务定义中引用属性来讲操作行为添加到服务描述中。

  类myParameterInspector实现了IParemeterInspector接口。类存储了两个本地属性,_pattern和 _message,用来在BeforeCall方法中验证参数。在那个方法中,使用正则表达式来匹配参数值和参数pattern.如果值与pattern 不符合,会抛出一个错误。

  类myOperationBehavior是吸纳了IEndpointBehavior和Attribute接口。在 AddDispatchBehavior方法中它添加myParameterInspector类到将要被每个操作调用的参数检测器列表中去。最后,当服 务操作时,GetPrice,被定义,myOperationBehavior属性用来在运行时验证它的参数。

列表5.24 在一个操作行为中以属性暴露的自定义参数检测器

01[AttributeUsage(AttributeTargets.Method)]
02public 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}
01class 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    }


======

转载自

 

转载于:https://www.cnblogs.com/llbofchina/archive/2011/06/30/2094101.html

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值