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    }


======

转载自

 

posted @ 2011-06-30 10:00  Gavin Liu  阅读(228)  评论(0编辑  收藏  举报

Right people get the right information at the right time.
以技术求生存,以市场求发展;学以至用,开拓创新;达技术之颠峰,至市场之广阔!