When throwing a FaultException<CustomFault> like this:
throw new FaultException<CustomFault>(new CustomFault("Custom fault message"));
I get the following: “The creator of this fault did not specify a Reason.”
Now according to this MSDN article one isn’t required to use the FaultReason
I have the following Service Contract:
[OperationContract]
[FaultContract(typeof(CustomFault))]
CustomType[] SomeMethod(int someParameter);
[DataContract]
public class CustomFault
{
private string report;
public CustomFault(string message)
{
this.report = message;
}
[DataMember]
public string Message
{
get { return this.report; }
set { this.report = value; }
}
}
There is a comment on the MSDN article that suggest it is mandatory to use the other FaultReason, but in several other places I have seen people arguing that you shouldn’t use the FaultReason unless necessary.
So my question is as follows; is it really mandatory to use the FaultReason and if not – how can i prevent the exception raised when trying to throw a FaultException?
EDIT
By running the sample project from this article I get the excact same behavior. Maybe it’s caused by an update in .NET an the documentation/samples not being updated.
The fault reason is required while the details are optional.
WCF uses the value of the FaultException.Reason property to populate the
<faultstring>element of the SOAP Fault, which is required by the SOAP specification:Hence, you must provide a reason for the fault when throwing a FaultException from a WCF service.