My WCF Service in C# looks like this.
[ServiceContract]
public class MySecretService
{
[OperationContract]
[FaultContract(typeof(ErrorMessage))]
public MyDTO ReturnMyDTOMethod(int id, out string errorMessage)
{
try
{
//Do stuff...
//Do some more stuff... pseudocode
if (biz rule 1 && etc)
{
return MyDTO;
}
else if (biz rule 2 && etc)
{
return null;
}
else
{
throw new FaultException<ErrorMessage>(blah etc..)
}
}
catch (Exception e)
{
throw new FaultException<ErrorMessage>(blah etc...);
}
}//end-method
}//end-class
The DTO to be returned from the method looks like this:
[DataContract]
public class MyDTO
{
[DataMember]
public XElement XmlRep
{
get
{
//do something within setter, etc...
//error occurs prior to returnin from setter, where to i catch it?
return _xmlRep
}
set
{
_xmlRep = value;
}
}
}
The typical examples that i found shows throwing a FaultException from within the method; But in my case, my method does not error; The error occurs at the time when the object is being returned to the client/consumer; i.e when the DataMember/Property XmlRep is being serialized;
So i cannot place the throw FaultException within my method; But i still want to avoid getting "The underlying connection was closed: The connection was closed unexpectedly." and throw the proper error that occurs within the getter.
I have not tried putting try/catch inside the getter of MyDTO, NOR DO I WANT TO BECAUSE i want my DTO to be as simple as possible, and know nothing about FaultExceptions and WCF stuff. Any other ideas ?
EDIT: Just to make it clearer, i know the error is occurring in the Getter of MyDto DataContract; But where else would i throw the FaultException, given that inside the Getter seems to me like a dodgy place to throw it?
EDIT#2: I implemented a catch-all error handler on the service side as suggested by Tim below (using IErrorHandler); this does not work in my specific case. I think this is because the Error does not occur within the OperationContract ReturnMyDTOMethod(), but instead within the MyDto when is being serialized; In other words, it appears the horse has bolted (method returns successfully), and it is to late for the IErrorHandler to be of any use - Specifically ProvideFault() does not fire but HandleError() does fire. Consequently i still get a channel broken message, which requires me to go back to the drawing board - i.e. ensure MyDto does not do anything fancy such as generate an error!