Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF Error Handling without fault contract

I have a WCF service which all operations return type is OperationStatus:

[DataContract]
public abstract class ServiceResponse
{
    [DataMember]
    public bool Success { get; set; }

    [DataMember]
    public String StatusReason { get; set; }

}

I'd like to create an ErrorHandler to catch all exceptions and then I'd like it to return to the client instance of class ServiceReponse with property Success set to false and StatusReason set to "INTERNAL SERROR".

As for now I have my own class implementing IErrorHandler but i don't wanna use FaultContract - i just want to return to the client regular object of type StatusReason. Can this be done?

like image 292
mgamer Avatar asked Mar 01 '23 00:03

mgamer


2 Answers

You do need a fault contract, in order to send over a Reason, try this:

   MyException fault = new MyException("Error message");
   throw new FaultException<MyException>(fault, new FaultReason("Reason Text"));
like image 82
Shiraz Bhaiji Avatar answered Mar 11 '23 01:03

Shiraz Bhaiji


Here is a nice article about handling exceptions in WCF

like image 38
Dani Avatar answered Mar 11 '23 02:03

Dani