WCF fault – The creator of this fault did not specify a reason


I was throwing a simple WCF FaultException yesterday and came across the same problem as this on the forums


 The client was trying to catch a fault as below:



catch (FaultException<InvalidOperationException> fexc)


{


MessageBox.Show(“A fault occurred because of an invalid operation exception”


 


To get it working from a server perspective I had to delcare the following:



InvalidOperationException oe = new InvalidOperationException(


“Debit amount is greater than balance in account “ + accountNumber);


FaultException<InvalidOperationException> fe = new FaultException<InvalidOperationException>(oe,


new FaultReason(“Debit amount is greater than balance in account “ + accountNumber));


throw fe;


I also marked the service interface with the following attribute:



[FaultContract(typeof(InvalidOperationException))]


 


I’m sure there must be a better way…. will followup 

Comments (2)

  1. an_phu says:

    Hi,

    What’s wrong with the code you have in the post?  

    An

  2. drmcghee says:

    The FaultException is a specific type of exception that allows bubbling up fault information into the WCF stack so that it can be mapped it to a SOAP fault. The code I was using did not populate the FaultException with a FaultReason (hence the updated code)