This documentation is archived and is not being maintained.

MessageFault Class

Represents an in-memory representation of a SOAP fault that can be passed to CreateMessage to create a message that contains a fault.

Namespace:  System.ServiceModel.Channels
Assembly:  System.ServiceModel (in System.ServiceModel.dll)

public ref class MessageFault abstract

Use the MessageFault class any time you need an in-memory SOAP fault that can be modified and used to create a SOAP message that contains the fault information.

Typically, the MessageFault class is used when implementing the IErrorHandler interface. In this case, Windows Communication Foundation (WCF) passes the MessageFault object and you use it for your specific needs (for example, you can customize the MessageFault or to log fault information). However, MessageFault can be used anywhere custom SOAP fault message handling is required.

The following code example shows the most common use of MessageFault. Both the ProvideFault and HandleError pass in a MessageFault object that can be modified and returned to the system (in the case of ProvideFault) or used to perform some custom fault-related behavior (in the case of HandleError).

In this example, the ProvideFault method converts all Exception objects into a MessageFault object that contains a FaultException<TDetail> object of type GreetingFault and returns that customized MessageFault to WCF.

#region IErrorHandler Members
public bool HandleError(Exception error)
  Console.WriteLine("HandleError called.");
  // Returning true indicates you performed your behavior. 
  return true;

// This is a trivial implementation that converts Exception to FaultException<GreetingFault>. 
public void ProvideFault(
  Exception error,
  MessageVersion ver,
  ref Message msg
  Console.WriteLine("ProvideFault called. Converting Exception to GreetingFault....");
  FaultException<GreetingFault> fe 
    = new FaultException<GreetingFault>(new GreetingFault(error.Message));
  MessageFault fault = fe.CreateMessageFault();
  msg = Message.CreateMessage(


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0

.NET Compact Framework

Supported in: 3.5