I have a WCF service with operations that all require MyServiceRequest parameter (or derived type) and returns MyServiceResponse (or dervived type), i.e.:
[OperationContract]
MySeviceResponse FindAppointments(FindAppointmentRequest request);
[OperationContract]
MyServiceResponse MakeAnAppointment(MakeAnAppointmentRequest request);
[OperationContract]
MyServiceResponse RegisterResource(RegisterResourceRequest request);
FindAppointmentRequest, MakeAnAppointmentRequest and RegisterResourceRequest extends MyServiceRequest which contains properties UserName and UserPassword.
None of this method executes properly should there be wrong UserName/UserPassword pair in Request.
I want to create an interceptor which not only checks if given UserName/UserPassword pair is correct (which is pretty simple with IParameterInspector) but also returns to the client object of type ErrorRespone that extends MyServiceResponse.
Can IParameterInspector prevent service from executing requested method and return ErrorResponse strightaway?
IParameterInspector can prevent operation from executing by throwing an exception. Here's an example.
Define a custom fault contract:
Then the inspector:
And finally decorate your operation with appropriate attributes:
When invoking the service, the client could check for FaultException:
You could have a method that returns a bool (True/False) that inspects your parameters at the entry of each of the above methods, then you can check if this has returned true or false and either return or execute the method accordingly.
So
Place this inside all your above methods... where CheckParams is your method from IParameterInspector to verify your parameters.