This documentation is archived and is not being maintained.

XmlSerializer Faults

Download sample

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.

Cc681334.Important(en-us,VS.90).gif Note:
The WCF samples may already be installed on your machine. Check this (default) directory before continuing: <InstallDrive>:\Samples\WCFWFCardspaceIf this directory doesn’t exist, click the download sample link at the top of this page. Note that this will download and install all of the WF, WCF, and CardSpace samples, you will only have to do this once. The sample is located in the following directory <InstallDrive>:\Samples\WCFWFCardSpace\WCF\Basic\Contract\Service\XmlSerializerFaults.

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 Set Up 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

© 2007 Microsoft Corporation. All rights reserved.