How to Validate Parameters with Parameter Inspectors
- J.D. Meier, Carlos Farre, Jason Taylor,
Prashant Bansode, Steve Gregersen, Madhu Sundararajan, Rob Boucher
Use the following steps to validate parameters with parameter inspectors:
- Create a Class That Implements the Validation Logic. This class has to derive from
IParameterInspector. In the class you will have the following characteristics:
- It implements AfterCall() and BeforeCall() methods. Those methods will have the validation logic.
- 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 the 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
- Create a Class That Implements a Custom Endpoint Behavior. This class derives from
IEndpointBehavior that the service and / or client endpoint will use as a configuration extensibility point for the endpoint. This class has the following characteristics
- It implements ApplyClientBehavior() to add the ParamaterInspector to the client operation and enable client-side validation.
- It implements ApplyDispatchBehavior() toadd the ParameterInspector to the dispatch operation and enable service-side validation.
- It verifies that it is enabled in the configuration before adding the ParameterInspector to the client or dispatch run time.
- Create a Class That Implements a Custom Configuration Element. This class derives from
BehaviorExtensionElement, This class allows to expose the endpoint configuration in WCF as a behavior element extension, that can be used by the service as an endpoint behavior configuration. This class has the e following characteristics:
- It implements CreateBehavior() to create an instance of the ValidationBehavior class.
- It implements BehaviorType() to return the ValidationBehavior type. This will allow the custom behavior to be exposed in the service or client configuration sections.
- Add the Custom Behavior to the Configuration File . You will add the custom behavior element to the behavior element extension items, so it can be used by the endpoint behavior. You add it in the configuration file with the configuration tool, by browsing
to the assembly, and selecting the type that is your custom behavior type.
- Create an Endpoint Behavior and Map It to Use the Custom Behavior. You will create a endpoint behavior to use the custom behavior extensible point provided by your assembly implementing the parameter inspector
- Configure the Service Endpoint to Use the Endpoint Behavior. You will configure the endpoint to use the endpoint behavior that is using the parameter inspector.