Monday, July 25, 2011

FaultException<T>

If you're stuck trying to get WCF to play nicely with a FaultException<T> where T is a custom System.Exception-derived type, you may be interested to know that the exception needs to have this protected constructor in order to work as expected:
[Serializable]
public class NotFoundException : ApplicationException {
public NotFoundException() {
}

protected NotFoundException(SerializationInfo info, StreamingContext context)
:base(info, context) {
}
}
Without it, WCF will not deserialize your custom exception properly, and you'll (at best) be able to catch a non-generic FaultException (with the rather unhelpful reason of "The creator of this fault did not specify a Reason."

No comments: