3

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?

Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112
Oscar
  • 13,594
  • 8
  • 47
  • 75
  • Maybe related: http://stackoverflow.com/questions/6588320/client-will-not-catch-generic-faultexception-t-only-faultexception – Ricardo Peres Jan 09 '17 at 14:02
  • @RicardoPeres Thanks, but I already have in my code fault = Message.CreateMessage(version, msgFault, faultEx.Action); as suggested in the accepted answer. – Oscar Jan 09 '17 at 14:51
  • Has ResponseModel [DataContract] applited? – Maximus Jan 11 '17 at 13:40
  • @Maximus Thanks for your time. Yes, I have the DataContract attribute set. – Oscar Jan 11 '17 at 14:49

0 Answers0