IParameterInspector может помешать выполнению операции, выдав исключение. Вот пример.
Определить пользовательский договор о неисправности:
public class DataAccessFaultContract
{
public string ErrorMessage { get; set; }
}
Тогда инспектор:
public class InspectorAttribute : Attribute, IParameterInspector, IOperationBehavior
{
public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)
{
}
public object BeforeCall(string operationName, object[] inputs)
{
MyServiceRequest request = null;
if (inputs != null && inputs.Length > 0)
{
request = inputs[0] as MyServiceRequest;
}
if (request != null && request.Username != "user" && request.Password != "secret")
{
var fc = new DataAccessFaultContract{ ErrorMessage = "Invalid user" };
throw new FaultException<DataAccessFaultContract>(fc);
}
return null;
}
public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
{
}
public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
{
dispatchOperation.ParameterInspectors.Add(this);
}
public void Validate(OperationDescription operationDescription)
{
}
}
И, наконец, украсьте вашу операцию соответствующими атрибутами:
[ServiceContract]
public interface IMyServiceContract
{
[Inspector]
[FaultContract(typeof(DataAccessFaultContract))]
[OperationContract]
MySeviceResponse FindAppointments(FindAppointmentRequest request);
...
}
При вызове службы клиент может проверить FaultException:
try
{
var response = client.FindAppointments(request);
}
catch (FaultException<DataAccessFaultContract> ex)
{
string errorMessage = ex.Detail.ErrorMessage;
// ...
}