In our application all data contract have one parent - OperationResponse and this OperationResponse contains enum (enum type OperationState in property Result) which represents calling result (Ok, InvalidParams, Error).
How can I overwrite WCF behavior after exception - not return Fault state, but return classic message, OperationResponse in my case, but with property Result = OperationState.Error?
Edit: I think, I'm on right way, I need to implement IErrorHandler, but now I'm lost, because I don't know how to set error message into outgoing fault message...
public bool HandleError(Exception error)
{
// log error
return true; // we catch error and provide our custom message.
}
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
// create result message
var message = new OperationResponse
{
Result = OperationState.Error
};
// ok how to return message into ref fault?
}
Edit2: It seems like it's unbreakable problem without solution... I only found same questions without solution like this thread... :(
I'm not sure this is exactly what you are looking for, but it is a fairly manageable way to handle errors from WCF (and probably other systems/contexts).
Here we create a generic
ServiceResponse<T>, which can contain any type of response in the propertyData, and status information inSucceededandStatusMessage.Use the
CreateServiceResponse()to sort of wrap which ever method you want to call inside this object. This way, you can return aServiceResponsewithDataset to the result from that method. There is an example of usage at the bottom.Perhaps you can adjust this to return an instance of your
OperationResponsewith anOperationStateinstead of theSucceededproperty used here?Example of usage (assuming GetClients() will return an IEnumerable):