views:

23

answers:

3

We are trying to figure out a way to modify WCF service behavior to catch all exceptions and instead of returning faults to the client, it will populate a custom return object with exception data and return that. So far, we haven't had much luck. I found this example: Catching custom faults

However, it doesn't return custom types as we would like it to. What other options are there?

Thanks!

A: 

I referred to this article in another answer, but it may help you as well.. http://stackoverflow.com/questions/3979010/wcf-exception-handling/3979662#3979662

the article is Simplifying WCF: Using Exceptions as Faults

zam6ak
A: 

If you want to have an interoperable and "by-the-standard" service, you should always return FaultException<T> SOAP faults from your service to the client.

Since that type takes a generic <T>, you can basically put anything into that type there to report back your errors. That type needs to be decorated with a [DataContract], and its members that need to be passed back with [DataMember] attributes.

[DataContract]
public class MyErrorInfo
{
    [DataMember]
    public int ErrorCode { get; set; }

    [DataMember]
    public string ErrorMessage { get; set; }
}

When you catch those execptions on the service side and return a FaultException<MyErrorInfo> (or whatever you'll end up calling your error class), you also need to decorate your operations with a

[FaultContract(typeof(MyErrorInfo))]
[OperationContract]
public SomeType SomeMethodCall(SomeType parameter);

so that your clients will be able to catch the FaultException<MyErrorInfo> and handle it.

marc_s
A: 

I wrote a blog post on this exact topic after we encountered this in our own project. Basically, we chose to return the same object type so we can attach a single listener delegate to all events to globally handle certain errors (like a user losing the permissions to an org.)

I hadn't thought of using FaultException but I will examine how we might do that. This design was WCF Service (.NET 3.5) running inside SharePoint 2007 and consumed by Silverlight 4.

Erik Noren