WCF在使用IParamaterInspector命中方法之前先验证Json请求

时间:2019-01-14 09:16:25

标签: wcf

我正在使用WCF for Mobile App为旧的API结构维护和开发新服务。我注意到字段的无效值(例如,十进制字段的字符串值)在反序列化步骤中引发了异常,然后才转到方法。我想在尝试方法之前使用IParamaterInspector处理验证。但是,即使执行了所有必需的步骤,它也不会触及BeforeCall方法。看来我错过了一个非常重要的步骤。

reflect.Value

该属性的用法如下,

public class ValidateParameterInspectorAttribute : Attribute, IParameterInspector, IOperationBehavior
{
    public object BeforeCall(string operationName, object[] inputs)
    {
        return null;
    }

    public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)
    {
    }


    public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
    {
    }


    public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
    {
        dispatchOperation.ParameterInspectors.Add(this);
    }


    public void Validate(OperationDescription operationDescription)
    {
        //throw new NotImplementedException();
    }


    public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
    {
        //throw new NotImplementedException();
    }
}

1 个答案:

答案 0 :(得分:0)

我进行了测试并取得了成功,但我将IParameterInspector和IOperationBehavior分开了

下面是我的代码。

我的模特。

 [DataContract]
public class Employee
{
    [DataMember]
    public string Id { get; set; }
    [DataMember]
    public string Name { get; set; }
    [DataMember]
    public string Department { get; set; }
    [DataMember]
    public string Grade { get; set; }
}

ParameterInspector

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

    }

public object BeforeCall(string operationName, object[] inputs)
    {
        Employee ee = inputs[0] as Employee;
            ee.Name = "myname";
        return inputs;
    }
}

OperationBehavior

  public class ParameterInspectorOperationBehavior :Attribute, IOperationBehavior
{
    public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
    {
    }

    public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
    {
        dispatchOperation.ParameterInspectors.Add(new MyParameterInspector());
    }

    public void Validate(OperationDescription operationDescription)
    {

    }
}

ServiceContract

 [ServiceContract]
 public interface IRestService
{
    [ParameterInspectorOperationBehavior]
 [WebInvoke(UriTemplate="/", Method = "POST", ResponseFormat =WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
    void Get(Employee employee);
}

服务配置

  <service name="Service.Rest.RestService">
    <endpoint address="http://localhost:3721/employee" contract="ServiceInterface.IRestService" binding="webHttpBinding"></endpoint>
  </service>

客户端配置

<endpoint  name="RestParameterInspector" address="http://localhost:3721/employee" contract="ServiceInterface.IRestService" binding="webHttpBinding"
           behaviorConfiguration="webBehavior"></endpoint>
 <endpointBehaviors>
    <behavior name="webBehavior">
      <webHttp />
    </behavior>
  </endpointBehaviors>

结果