I have implemented ProvideFault
method of IErrorHandler
interface as follows:
public void ProvideFault(Exception ex, MessageVersion version, ref Message fault)
{
ResponseModel responseModel = new ResponseModel();
responseModel.EndDate = DateTime.Now;
FaultException<ResponseModel> faultEx;
if (ex is FaultException<ResponseModel>)
{
return;
}
ServerCoreException coreException = ex as ServerCoreException;
if (coreException != null)
{
ServerCoreException serverCoreException = coreException;
faultEx = new FaultException<ResponseModel>(responseModel,
new FaultReason(serverCoreException.Message),
new FaultCode(serverCoreException.ErrorCode.ToString()));
}
else
{
faultEx = new FaultException<ResponseModel>(responseModel,
new FaultReason(ex.Message),
new FaultCode("-1"));
}
MessageFault msgFault = faultEx.CreateMessageFault();
fault = Message.CreateMessage(version, msgFault, faultEx.Action);
}
Then I add this implementation to every ChannelDispatcherBase
in another implementation of IServiceBehavior
called GlobalErrorBehaviorAttribute
. My service implementations are decorated with this attribute and also have FaultContractAttribute
as:
[FaultContract(typeof(ResponseModel), Name = "ServiceFaultException",
Namespace = "http://ws.net.core/base/model")]
But test for methods inside service implementation are always failing complaining that the received fault is not of type FaultException<ResponseModel>
but FaultException
instead:
Assert.That(() => client.CreateVersion(request), Throws.TypeOf<FaultException<ResponseModel>>());
Why am I receiving FaultException
instead of FaultException<ResponseModel>
in client side?