XmlSerializer Faults

The XmlSerializer fault contract 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.


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.

public interface ICalculator  
    int Add(int n1, int n2);  

    int Subtract(int n1, int n2);  

    int Multiply(int n1, int n2);  

    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.


The samples may already be installed on your machine. Check for the following (default) directory before continuing.


If this directory does not exist, go to Windows Communication Foundation (WCF) and Windows Workflow Foundation (WF) Samples for .NET Framework 4 to download all Windows Communication Foundation (WCF) and WF samples. This sample is located in the following directory.


See Also