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 Sample, 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.

Cc681334.Important(en-us,VS.100).gif Note:
The samples may already be installed on your machine. Check for the following (default) directory before continuing.

<InstallDrive>:\WF_WCF_Samples

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.

<InstallDrive>:\WF_WCF_Samples\WCF\Basic\Contract\Service\XmlSerializerFaults

See Also

Reference

XmlSerializerFormatAttribute
SupportFaults