• WCF中校验参数的实现方式(一)


       在用WCF写服务程序时,有时候需要做一些参数的校验,当然这些参数校验可放到调用端处理,但服务为了保险起见也需要有对参数的校验处理。如果服务不多,那么可直接在服务的方法中去校验,然后直接扔个异常或者返回错误结果。但这种针对单个方法写得校验不可重用,希望能有一种类似model校验的方式,加个标记或者属性配置一下就可以。毕竟校验这块不是WCF的重点内容,将校验代码放入到服务实现中也不太合理。WCF提供了一些扩展的地方,可以在运行时去校验参数是否合法,通过定义一些behavior,然后加在需要校验的方法上就可实现对方法参数的校验。

    下面说说WCF中两种校验输入参数的实现方法。

    方法一:

    自定义校验方法的behavior,然后将这个behavior作为属性标记在接口的方法上。具体涉及到的接口有IParameterInspector,IOperationBehavior。

    IParameterInspector

    很明显就是用于对参数的检测,具体对参数的检测都是在这个接口的BeforeCall方法中实现。下面定义的是一个对参数长度的校验实现,当校验失败后通过返回异常的方式传递错误信息。其中ParameterIndex是参数的索引,MaxLength只参数的最大长度。

    LengthParameterInspector
     1 public class LengthParameterInspector : IParameterInspector
     2     {
     3         public int ParameterIndex { getprivate set; }
     4 
     5         public int MaxLength { getprivate set; }
     6 
     7         public LengthParameterInspector(int parameterIndex, int maxLength)
     8         {
     9             this.ParameterIndex = parameterIndex;
    10             this.MaxLength = maxLength;
    11         }
    12 
    13         public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)
    14         {
    15             //throw new NotImplementedException();
    16         }
    17 
    18         public object BeforeCall(string operationName, object[] inputs)
    19         {
    20             if (inputs.Length < ParameterIndex)
    21             {
    22                 return null;
    23             }
    24 
    25             string input = inputs[ParameterIndex] as string;
    26 
    27             if (input != null)
    28             {
    29                 if (input.Length > this.MaxLength)
    30                 {
    31                     throw new FaultException(string.Format("the length of parameter {0} must less than {1}", ParameterIndex.ToString(), MaxLength.ToString()));
    32                 }
    33             }
    34 
    35             return null;
    36         }
    37     }

    IOperationBehavior

    就是用于方法的行为接口,通过ApplyDispatchBehavior方法加入实现了IParameterInspector的实例(红色部分代码)。实际上是一个Attribute,那么就可以直接用在服务的方法中。

    LengthValidateBehavior
     1 [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
     2     public class LengthValidateBehavior: Attribute, IOperationBehavior
     3     {
     4         private int parameterIndex;
     5         private int maxLength;
     6 
     7         public LengthValidateBehavior(int parameterIndex, int maxLength)
     8         {
     9             this.parameterIndex = parameterIndex;
    10             this.maxLength = maxLength;
    11         }
    12 
    13         public void AddBindingParameters(OperationDescription operationDescription, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
    14         {
    15             //throw new NotImplementedException();
    16         }
    17 
    18         public void ApplyClientBehavior(OperationDescription operationDescription, System.ServiceModel.Dispatcher.ClientOperation clientOperation)
    19         {
    20             //throw new NotImplementedException();
    21         }
    22 
    23         public void ApplyDispatchBehavior(OperationDescription operationDescription, System.ServiceModel.Dispatcher.DispatchOperation dispatchOperation)
    24         {
    25             dispatchOperation.ParameterInspectors.Add(new LengthParameterInspector(parameterIndex, maxLength));
    26             //throw new NotImplementedException();
    27         }
    28 
    29         public void Validate(OperationDescription operationDescription)
    30         {
    31             //throw new NotImplementedException();
    32         }
    33     }

    具体在方法中的使用如下,即校验第一个参数的长度,最大长度不能超过10个字。

    View Code
    1     [ServiceContract]
    2     public interface ITradeService
    3     {
    4         [OperationContract]
    5         [LengthValidateBehavior(010)]
    6         string TradeSomething(string quoteName);
    7     }

    完成以上步骤之后,就可实现简单的参数校验。但这种方式对于多参数时,不好使,因为WCF中不支持对同一个方法定义多个相同类型的Behavior,即使Attribute中指定了AllowMultiple=true也无效。

    下面介绍第二种方法,借助企业库的校验模块实现对参数校验。

    方法二:

     后续更新

  • 相关阅读:
    安装oh-my-zsh
    Ubuntu下安装2017版QQ
    Ubuntu安装Git
    链接libtorrent库时出现的问题
    ubuntu 下重装mysql若干问题
    最简单的epoll的使用范例 : 监听 标准输入 ,并将数据回显到终端
    [转]Linux下CodeBlocks的交叉编译
    各种免费素材下载站点
    Qt5:图片彩色键控,设置图片中指定颜色的像素为透明
    C++:预处理指令
  • 原文地址:https://www.cnblogs.com/shenba/p/2305413.html
Copyright © 2020-2023  润新知