I am trying centralize the Exception handling in my WCF service by using a ErrorHandler
(System.ServiceModel.Dispatcher.IErrorHandler
)
This is how the ProvideFault look like :
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
if (error is ValidationHandler)
{
var val = error as ValidationHandler;
var val2 = ErrorHandler.HandleException(val);
fault = Message.CreateMessage(version, val2.CreateMessageFault(), "http://ns");
}
else if (error is FaultException)
fault = Message.CreateMessage(version, (error as FaultException).CreateMessageFault(), "http://ns");
else
fault = Message.CreateMessage(version, ErrorHandler.HandleException(error).CreateMessageFault(), "http://ns");
}
The ValidationHandler
is a System.Exception
but do not have datacontract
attribute. ValidationHandler
contains a list of MainValidation
where all data is stored and this class is decorated with datacontract
attribute.
ErrorHandler.HandleException
will translate the ValidationHandler
to a FaultException<MyCustomFault>
that is a custom FaultException
containing ExceptionContainer
(datacontract
) containing the list of MainValidation
(datacontract
).
If I remove the IErrorHandler
and just throws the ErrorHandler.HandleExceptio
n direcly from the service method everything works fine. The CustomFault as arriving at the client and the validation can be handled.
But while using IErrorHandler
the fault that arrive at the client can“t be casted to a FaultException<MyCustomFault>
? It seems like data is lost when running the Message.CreateMessage
?
Edit 1 : I can see that the MessageFault that are generated from CreateMessageFault do have a detail and if I cast this to my MyCustomFault I can read the content properly. The question is why this is not included when arriving at the client?
Edit 2 : When the exception arrives at the client I try to cast it to FaultException but this returns null. So the MyCustomFault seems to be lost on the way?
Edit 3 : I have the following set on the behavior in the config :
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceMetadata httpGetEnabled="true" />