While I3arnon answer is good if you want all exceptions to be forwarded on to the callers, if you want only a limited set of known faults to come through you can create Fault Contracts which let the caller know a specific set of exceptions may be coming through so the client can be ready to handle them. This allows you to pass along potential expected exceptions without forwarding on all exceptions your software may throw on to the client.
Here is a simple example from the MSDN that shows a WCF service catching a DivideByZeroException
and turning it in to a FaultException
to be passed along on to the client.
[ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples")]
public interface ICalculator
{
//... (Snip) ...
[OperationContract]
[FaultContract(typeof(MathFault))]
int Divide(int n1, int n2);
}
[DataContract(Namespace="http://Microsoft.ServiceModel.Samples")]
public class MathFault
{
private string operation;
private string problemType;
[DataMember]
public string Operation
{
get { return operation; }
set { operation = value; }
}
[DataMember]
public string ProblemType
{
get { return problemType; }
set { problemType = value; }
}
}
//Server side function
public int Divide(int n1, int n2)
{
try
{
return n1 / n2;
}
catch (DivideByZeroException)
{
MathFault mf = new MathFault();
mf.operation = "division";
mf.problemType = "divide by zero";
throw new FaultException<MathFault>(mf);
}
}