1

I have a Custom class, InvalidCodeException in Project A

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

        public InvalidCodeException (string message, Exception innerException)
            : base(message, innerException)
        {

        }
    }

a WCF service in Project B. And a client in Project C. Project A is referenced in Project B and C.

I am throwing InvalidCodeException from Project B and catching in Project C. Problem is that when debuggin, the exception is not catching in

catch (InvalidCodeException ex)
{
  Trace.WriteLine("CustomException");
  throw;
}

but in

catch (Exception ex)
{ throw; }
Hataf Moin
  • 161
  • 1
  • 2
  • 9
  • how are you throwing InvalidCodeException. Show the code where you are throwing it – Ehsan Aug 20 '13 at 09:30
  • if (productCode == null) throw new CustomException("Invalid code.");This is in WCF service – Hataf Moin Aug 20 '13 at 13:47
  • it should be throw new InvalidCodeException("Invalid code") – Ehsan Aug 20 '13 at 15:51
  • @NoOne it is InvalidCodeException("Invalid code"),mistakenly written CustomException. – Hataf Moin Aug 21 '13 at 06:19
  • seems like the same issue im having. http://stackoverflow.com/questions/21712231/custom-exception-thrown-by-a-web-service-not-seen-as-same-type-by-the-client-us/21762657?noredirect=1#21762657 maybe some of their comments will help you. i have not figured it out yet – owen gerig Feb 18 '14 at 18:36

3 Answers3

2

WCF will not serialize exceptions automatically, for many reasons(e.g. client may be written in some other language than C#, and run on platform that is different from .NET).

However, WCF will serialize some exception information into FaultException objects. This basic information contains exception class name, for example. You may store some additional data inside them if you use generic FaultException type. I.e. FaultException<FaultInfo> where FaultInfo is your class that stores additional exception data. Don't forget to add data contract serialization attributes onto class properties.

Also, you should apply FaultContract attributes on methods that are supposed to throw FaultExceptions of your kind.

[OperationContract]
[FaultContract(typeof(FaultInfo))]
void DoSomething();
lovesan
  • 96
  • 3
1

That's because all exceptions are wrapped in FaultException. Catch it and look at the InnerException property

MSDN Article

jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • Not strictly true - this only works if you set IncludeExceptionDetailInFaults property to true in the service config - this property is false by default to prevent service implementation details from leaking across the service boundary. – tom redfern Aug 20 '13 at 13:10
1

The correct way to handle this is to define a FaultContract on your service operation contract:

[OperationContract]
[FaultContract(typeof(MyException))]
int MyOperation1(int x, int y);

This will allow your client to handle the exception by catching it as a generic FaultException:

try {...}
catch (FaultException<MyException> e) {...}
tom redfern
  • 30,562
  • 14
  • 91
  • 126
  • I have tried this and the catch sequence is (FaultException ex) { throw; } catch (FaultException ex) { throw; } catch (Exception ex){throw;} The exception is not catching in first catch but in second catch.I have thrown exception from service like this InvalidCodeException fault = new InvalidCodeException ("Invalid product code."); throw new FaultException(fault, new FaultReason("Invalid product code.")); – Hataf Moin Aug 21 '13 at 15:18
  • You should just be able to throw an instance of your InvalidCodeException. – tom redfern Aug 21 '13 at 15:42