First off, I'm a student and new to WCF and C# in general. I'm trying to demonstrate the response returned to a client when a typed WCF FaultException occurs. I have a typed exception defined like so:
[DataContract]
public class CustomFault {
[DataMember]
public string cause;
[DataMember]
public string additionalInfo1;
[DataMember]
public string additionalInfo2;
public CustomFault(string cause, string additionalInfo1, string additionalInfo2) {
this.cause = cause;
this.additionalInfo1 = additionalInfo1;
this.additionalInfo2 = additionalInfo2;
}
public override string ToString() {
return string.Format("\ncause: {0}\nadditionalInfo1: {1}\nadditionalInfo2: {2}\n", this.cause, this.additionalInfo1, this.additionalInfo2);
}
}
And I have a service which simply throws a FaultException<CustomFault>
when called using this line:
throw new FaultException<CustomFault>(new CustomFault("Specified cause", "additional info 1", "additional info 2"));
I have a test client which invokes the service method, catches the exception and prints the exception in a message box:
try {
exampleServiceClient.causeTypedFaultException();
}
catch (FaultException<CustomFault> faultException) {
MessageBox.Show(faultException.ToString());
}
My problem is the result doesn't include the CustomFault
details (i.e. cause
, additionalInfo1
or additionalInfo2
fields) despite the client successfully receiving them (visible when debugging). I can't figure out why since I have overridden ToString
to print these fields. I'm aware I can access the fields individually but I would like to know why this approach doesn't work.
I'd really appreciate any help anyone could give.
Edit: The result just contains this:
The creator of this fault did not specify a reason. (FaultDetail is equal to CustomFault).
Edit 2: Service method definition:
[OperationContract]
[FaultContract(typeof(CustomFault))]
void causeTypedFaultException();