Gary
Gary

Reputation: 33

WCF typed FaultException toString() not being used?

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();

Upvotes: 1

Views: 385

Answers (1)

anton.burger
anton.burger

Reputation: 5706

Related question

I wasn't aware that FaultException<T> used the ToString method of its detail type, but the .NET reference source seems to suggest it should. Useful to know!

Does your client use the original type definition of CustomFault, or is it using a version generated from the service metadata (e.g. using "Add Service Reference", without choosing the option to reuse types in referenced assemblies)? The latter is the more common situation, because it's the general "interoperate with clients written in other languages" case. It'll generate a client-side CustomFault type ("Add Service Reference" hides this in a special Reference.cs source file) that contains all the fields and properties marked as DataMembers (i.e. enough to produce and parse the wire representation), but the service metadata doesn't provide enough information to regenerate any other type members - there's no way to represent a specific language's ToString method implementation using WSDL, for example, because it's too specific for interoperability across languages and platforms. So the client-side CustomFault gets no overridden ToString.

Upvotes: 0

Related Questions