4

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?

mgamer
  • 13,580
  • 25
  • 87
  • 145
  • 1
    Is there a reason why you don't want to use the fault contract? A fault is essentially the wire-friendly way of throwing an exception... – mkedobbs Nov 27 '09 at 18:53
  • 2
    if you want to be interoperable, e.g. be callable from something else but .NET, you **have to** use SOAP faults (FaultException) - that's what they were designed for. Why do you need to reinvent the wheel and swim against the current?? Use those things provided for you - it'll be a lot less hassle ..... – marc_s Nov 27 '09 at 21:25
  • WCF can be used by client that is not .NET. Standard error handling might not be the best solution in such a case. – Fedearne Nov 28 '09 at 12:37

3 Answers3

4

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"));
Shiraz Bhaiji
  • 64,065
  • 34
  • 143
  • 252
2

Here is a nice article about handling exceptions in WCF

Dani
  • 14,639
  • 11
  • 62
  • 110
  • 1
    why??? FaultException are the mechanism for handling error conditions in WCF - just like exceptions are the mechanism for .NET – marc_s Nov 27 '09 at 21:25
2

You could add

 <serviceDebug includeExceptionDetailInFaults="true"/>

To your service behaviors section but it isn't really the best for production environment.

MattC
  • 3,984
  • 1
  • 33
  • 49