1

I have a MVC project that makes a call to a WCF service. The WCF service method, in the case of business rule failure, throws its own exception, CarTooOldException. This has a string that I want to show to the user, "Car is too old".

public class CarTooOldException : Exception
{
    public CarTooOldException(string message) : base(message)
    { }
}

My MVC code will catch an exception but the type is a FaultException. The message inside is correct however ("Car is too old").

Without changing the WCF service, is there a way to detect that this is a CarTooOldException? A call to .GetType() doesn't tell me.

How can I be sure that if I see this FaultException that the message is a user-friendly one and safe to show the user?

Right now I fear my only choice is to look a the strings the WCF method throws and do a .Contains() on the message property.

EDIT: The method literally does

if (age > MAX_AGE)
    throw new CarTooOldException("Car is too old")

Thanks to @3dd for pointing me at that SO question. I understand that I cannot determine what the type is, because the developer that wrote the WCF service did not do FaultException.

I shall be looking to do a string comparison instead, as I don't want to change the service (legacy code).

VictorySaber
  • 3,084
  • 1
  • 27
  • 45
  • This should point you into the right direction http://stackoverflow.com/questions/16045726/catching-a-specific-wcf-faultexception – 3dd Aug 11 '15 at 11:51
  • 2
    1) Do you do `throw new FaultException(new CarTooOldException("..."))` in your code? 2) Have you declared `[FaultContract(typeof(CarTooOldException))]` on the respective service operation. In case of doubt, please show us the relevant code. – Christian.K Aug 11 '15 at 11:52

0 Answers0