http://msdn.microsoft.com/en-us/library/ff647875.aspx
- It implements AfterCall() and BeforeCall() methods.
- When used as part of the service, BeforeCall() 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.
- When used as part of the client, BeforeCall() 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>