XmlSerializer Faults

The XmlSerializerFaults sample demonstrates how to communicate error information from a service to a client using the XmlSerializer. The sample is based on the Getting Started, with some additional code added to the service to convert an internal exception to a fault. The client attempts to perform division by zero to force an error condition on the service.

Note

The setup procedure and build instructions for this sample are located at the end of this topic.

The calculator contract has been modified to include a FaultContractAttribute as shown in the following sample code. Also, the XmlSerializerFormatAttribute is used to enable serialization using the XmlSerializer. The SupportFaults property is set to true on this attribute, which instructs the serializer to use the XmlSerializer for reading and writing faults.

[XmlSerializerFormat(SupportFaults=true)]
[ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples")]
public interface ICalculator
{
    [OperationContract]
    int Add(int n1, int n2);

    [OperationContract]
    int Subtract(int n1, int n2);

    [OperationContract]
    int Multiply(int n1, int n2);

    [OperationContract]
    [FaultContract(typeof(MathFault))]
    int Divide(int n1, int n2);
}

When generating code for the client proxy, you must apply the /UseSerializerForFaults flag to ServiceModel Metadata Utility Tool (Svcutil.exe).

To set up, build, and run the sample

  1. Ensure that you have performed the One-Time Setup Procedure for the Windows Communication Foundation Samples.

  2. To build the C# or Visual Basic .NET edition of the solution, follow the instructions in Building the Windows Communication Foundation Samples.

  3. To run the sample in a single- or cross-machine configuration, follow the instructions in Running the Windows Communication Foundation Samples.

See also