Perform Input Validation in WCF

http://msdn.microsoft.com/en-us/library/ff647875.aspx

  1. It implements AfterCall() and BeforeCall() methods.
  2. When used as part of the serviceBeforeCall() will be invoked before the parameters are dispatched to the service operation.AfterCall() will be invoked after the service has processed the call and is returning a response to the client. Use BeforeCall() to validate your input parameters and AfterCall() to validate your output parameters.
  3. When used as part of the clientBeforeCall() will be invoked before calling the service, and AfterCall() before the service's response is dispatched to the client code. Use AfterCall() to validate the response from the service, and BeforeCall() to validate the return from the service.

 public class ValidationParameterInspector : IParameterInspector
        {
            public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)
            {

                if (operationName == "GetData")
                {
                    for (int index = 0; index < outputs.Length; index++)
                    {
                        if (index == 0)
                        {

                            // execute the method level validators
                            if (((int)outputs[index] < 0) || ((int)outputs[index] > 5))
                                throw new FaultException("Your Error Message");
                        }

                    }

                }
            }
        public object BeforeCall(string operationName, object[] inputs)
            {

                if (operationName == "GetData")
                {

                    for (int index = 0; index < inputs.Length ; index++)
                    {
                        if(index==0)
                        {
                        // execute the method level validators
                        if (((int)inputs[index]<0) ||  ((int)inputs[index] > 5))
                            throw new FaultException("Validation Input  Error");
                        }

                    }

                }

                return null;
            }
}


Client Side

SimpleServiceClient proxy = new SimpleServiceClient();
proxy.Endpoint.Contract.Operations[0].Behaviors.Add(new MyParameterInspector());

Service Side

+ Create a Class That Implements a Custom Endpoint Behavior +Custom Behavior Extension 

<system.serviceModel>
  ...
    <extensions>
      <behaviorExtensions>
        <add name="Validator" type="MyParamaterValidator. CustomBehaviorSection, MyParamaterValidator, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
      </behaviorExtensions>
   </extensions>
 ... 
 <system.serviceModel>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值